Profile Lookup passes but I feel like it's still wrong

Profile Lookup passes but I feel like it's still wrong
0

#1

The last bit where I use an if statement to return “No such contact” is confusing me. The only way I could get the whole thing to pass is by pulling that last if statement out of the loop. But it seems as if maybe this is not correct? The reason I’m saying this is because to me, in my beginner programming brain sees that statement as being outside the loop, so it isn’t actually picking up what the loop is outputting. The statement would be false no matter what so it could pass the test no matter what. I dunno…maybe I’m overthinking this. It passed, I should be happy. What are your thoughts?

Thanks for any help! :slight_smile:

//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 ((contacts[i].firstName === (firstName)) && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].hasOwnProperty(prop) === false) {
return “No such property”;
}
}
if (contacts.firstName != firstName) {
return “No such contact”;
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile(“Kristian”, “lastName”);


#2

I think putting no such contact oustide of the for-loop is natural. If you think about it, after looking up each contact and found no contact with the specified property, then it’s safe to say that there’s no such contact. You don’t even need the if-block around that statement :slight_smile: