OK so for the profile lookup challenge i wrote this code
//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(firstName, prop){
// Only change code below this line
for(var i=0;i<=contacts.length;i++){
if(firstName==contacts[i].firstName){
if(contacts[i].hasOwnProperty(prop) === true){
return contacts[i][prop];
}else{
return "No such property";
}
}
}
return "No such contact";
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Bob", "likes");
NOTE: it will work with all cases as long as there is a valid firstname input. So it will work
even with a invalid prop input.
Which I know it’s not as optimized as it could be.
Because as the amount of things you look through gets bigger it would get slower and slower
to return something but any way. I assume its throwing a problem in that its not ending the
for loop and returning “No such contact”. But I have no idea why. My console error is
TypeError: cannot read property ‘firstname’ of undefined
Any help or a different way of solving this challenge would be great!