Tell us what’s happening:
Hi guys. I’m on the mentioned JS challenge and my code doesn’t run completely, it does solve returning “no such contact” and “no such property,” but it doesn’t return the properties given.
I’ve seen multiple answers already, but they work by checking first whether the contact exists and then if the contact has x property. I can’t wrap my head around this… my (clearly wrong) logic follows:
- Use a for to sort through the indexes of the array.
-
- Then compare the index(es) with the given function name. If there’s not such a name, then it doesn’t exist and thus return me the string saying that.
-
-
- If it does exist, compare the properties in that nested array with the prop given by the function. If there’s no match then it doesn’t exist, return me a string saying tht.
-
-
-
-
- Given that both the name is in the contact list and it has the property, then return both to me!
-
-
I don’t know where I am wrong. I could go with the working answers, but I’m curious why some part works and the last doesn’t, I was thinking mabe it has something to do with the if else if else logic, but it doesn’t seem bad to me, I’m lost lost lost. I tried to write it down on paper and draw the variables going up and down but it just got me even more confused.
Any help is appreciated.
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(contacts[i]["firstName"] != name) {
return "No such contact";
} else if (contacts[i][prop] != prop) {
return "No such property";
} else {
return contacts[i][prop];
}
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Profile Lookup
Link to the challenge: