I know it’s messy and long but I want to do it my own noobish way before learning the proper way. Just how I operate. I can’t figure out why this doesnt work.
My 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(name, prop) {
for (let i = 0; i < contacts.length; i++) {
//below is what I'm having trouble with
if (name == contacts[i].firstName && prop == contacts[i].lastName) {
return contacts[i].lastName;
} else if (name == contacts[i].firstName && prop == contacts[i].likes) {
return contacts[i].likes;
//above is what I'm having trouble with.
} else if (name !== contacts[i].firstName) {
return "No such contact";
} else if (prop !== contacts.[i][prop]) {
return "No such property";
}
}
}
lookUpProfile("Akira", "likes");
When I console.log contacts[i].lastName or contacts[i].likes I get the proper output. So why doesn’t it accept my answers?