Freecodecamp’s console is giving me “TypeError: Cannot read property ‘firstName’ of undefined” in the following code even though I can console.log(contacts[i].firstName) in the same block:
// Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"]
}
];
function lookUpProfile(name, prop){
// Only change code below this line
var contactExists;
for(var i = 0; i <= contacts.length; i++){
console.log(contacts[i].firstName);
if (name !== contacts[i].firstName) {
contactExists = false;
} else {
contactExists = true;
};
}
if (!contactExists) {
return "No such contact";
} else {
return contactExists;
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");