Hi.
I’am working on the JavaScript basics and I got stuck at the “Profile Lookup”.
It looks like my for loop does not iterate through the data. If I want to look up for someone other that Akira I only get the “No such contact” output. Can somebody please point out what the problem is here? Thank you.
//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"]
},
{
"firstName": "Christiano",
"lastName": "Vozzo",
"number": "unknown",
"likes": ["Javascript", "GangBang", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
for(i = 0; i < contacts.length; i++){
if(contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop) === true){
return contacts[i][prop];
}
else if(contacts[i].firstName !== firstName){
return "No such contact";
}
else if(contacts[i].hasOwnProperty(prop) === false){
return "No such property";
}
else{
return "Why don't you iterate?";
}
}
}
// Change these values to test your function
lookUpProfile("Kristian", "lastName");
lookUpProfile("Akira", "likes");
lookUpProfile("Akira", "lastName");
lookUpProfile("Kristian", "lastName");