0

I'm reading chapter 5 of Eloquent JavaScript: A Modern Introduction to Programming Book. I found this JSON online and my goal is to exercise what I learned from the book. I have some background on Java however because JavaScript is a loosely typed language declarations and parsing are a bit confusing. The code in coma does compile but what I have inside loop doesn't. Though it's obviously wrong, to my logic that line of code shouldn't make a different inside of loop or outside loop. Is the logic wrong or the syntax?

The Error i'm getting is:

Cannot read property 'died' of undefined

var DATA = "[\n  " + [
'{"name": "Carolus Haverbeke", "sex": "m", "born": 1832, "died": 1905, 
"father": "Carel Haverbeke", "mother": "Maria van Brussel"}',
'{"name": "Emma de Milliano", "sex": "f", "born": 1876, "died": 1956, 
"father": "Petrus de Milliano", "mother": "Sophia van Damme"}',
'{"name": "Maria de Rycke", "sex": "f", "born": 1683, "died": 1724, 
"father": "Frederik de Rycke", "mother": "Laurentia van Vlaenderen"}',
'{"name": "Jan van Brussel", "sex": "m", "born": 1714, "died": 1748, 
"father": "Jacobus van Brussel", "mother": "Joanna van Rooten"}',
'{"name": "Philibert Haverbeke", "sex": "m", "born": 1907, "died": 1997, 
"father": "Emile Haverbeke", "mother": "Emma de Milliano"}',
'{"name": "Jan Frans van Brussel", "sex": "m", "born": 1761, "died": 1833, 
"father": "Jacobus Bernardus van Brussel", "mother":null}',
'{"name": "Pauwels van Haverbeke", "sex": "m", "born": 1535, "died": 1582, 
"father": "N. van Haverbeke", "mother":null}',
'{"name": "Clara Aernoudts", "sex": "f", "born": 1918, "died": 2012, 
"father": "Henry Aernoudts", "mother": "Sidonie Coene"}',
'{"name": "Emile Haverbeke", "sex": "m", "born": 1877, "died": 1968, 
"father": "Carolus Haverbeke", "mother": "Maria Sturm"}',
'{"name": "Lieven de Causmaecker", "sex": "m", "born": 1696, "died": 1724, 
"father": "Carel de Causmaecker", "mother": "Joanna Claes"}',
'{"name": "Pieter Haverbeke", "sex": "m", "born": 1602, "died": 1642, 
"father": "Lieven van Haverbeke", "mother":null}',
'{"name": "Jacobus Bernardus van Brussel", "sex": "m", "born": 1736, "died": 
1809, "father": "Jan van Brussel", "mother": "Elisabeth Haverbeke"}'
].join(",\n  ") + "\n]";

if (typeof module != "undefined" && module.exports)
    module.exports = DATA;

function Search() {
    // console.log(result[1].born);
    var result = JSON.parse(DATA);
    var a1,a2;

    for(var i=0; i<result.length;i++)
    {
        a2= +result[i].died;
        a1 = +result[i].born; 

        if(a1 >= 1800 && ((a2-a1) <=40))
        {   
            console.log(result[i].name);    
        } 
    } 
}
4
  • 1
    it's died not dead... Commented Oct 23, 2017 at 0:28
  • made the change, same issue, the property can't be read. Commented Oct 23, 2017 at 0:32
  • i <= result.length -> i < result.length Commented Oct 23, 2017 at 0:35
  • That makes sense. Commented Oct 23, 2017 at 0:40

2 Answers 2

1

1 - It's not "dead" but "died", you just misreaded..

2 - Try to filter

console.log(result[i]);
if(result[i].died && result[i].born){
   a2=  +result[i].died;
   a1 = +result[i].born;
}

3 - your loop goes too far, change it for(var i=0; i < result.length - 1;i++)

Sign up to request clarification or add additional context in comments.

8 Comments

Please add comment, not sure what you just did. Haven't seen +result.... in the book.
+ is a shortway to parseInt
Interesting, what did you mean by filter? the filter method?
You can accept my answer with the check icon at left ;-) Thx.
are you sure my logic is right?, the result I'm getting is wrong
|
0

You have an off by one error

for(var i=0; i<=result.length;i++)
{
   ...
} 

Your array has 12 items, but the loop runs 13 times.

for(var i=0; i<=result.length - 1 ;i++)

or

for(var i=0; i < result.length ;i++)

1 Comment

True, didn't see that. Thought about changing it to i=1 but array index starts by 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.