Tell us what’s happening:
Ok so I’m struggling with another one of these. I can’t seem to get my head around how I am supposed to get the function to look past the first ‘object’ in the array.
I thought since the prior lesson were about Loop iteration I should use that but it doesn’t work. The only name
that works is the first one. I can get Akira and the properties to work with that name only. Any other name gives me a ‘No such Contact’.
I would expect by running
console.log(lookUpProfile("Harry", "likes"));
That it would return the value of ‘likes’ but it doesn’t.
Here’s the code so far:
Your code so far
// 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
for (var i = 0; i < contacts.length; i++) {
if (name == contacts[i].firstName) {
return contacts[i][prop] || contacts[i][prop].values();
}else if (name == contacts[i].firstName && prop != contacts[i][prop]) {
return "No such property";
}else {
return "No such contact";
}
}
// Only change code above this line
}
console.log(lookUpProfile("Harry", "likes"));
Please help me understand why my function is broken. Thank you.
Your browser information:
User Agent is: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 EdgiOS/45.3.19 Mobile/15E148 Safari/605.1.15
.
Challenge: Profile Lookup
Link to the challenge: