Hi there!
This from the code of challenge 210 “Profile Lookup”, of which the discussion was closed recently.
There is a standard solution, looking like this:
for ( i =0; i< contacts.length; i++){
if (firstName===contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
And there was my solution, looking like this:
for ( i =0; i<= contacts.length; i++){
if (firstName===contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
You’ll notice that the only difference between those two is in the following lines:
1.for ( i =0; i< contacts.length; i++){
2.for ( i =0; i<= contacts.length; i++){
In the second I used “<=” instead of a plain “<”. This resulted in an Error “TypeError : Cannot read property 'fristName of undefined”
I don’t understand why my code shouldn’t work, or why it resulted in a TypeError.
Any help or explanation will be greatly appreciated!