Profile Lookup - wrong solution

Hi guys,

I was trying to solve Profile Lookup with something like this:

function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
    if (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
    else if (name !== contacts[i]["firstName"]) {
        return "No such contact"
    else {
        return "No such property"
// Only change code above this line

But it did not pass. Eventually, I looked at the hint telling me to nest my if statements, but I still don’t understand why the code above did not work. Anybody can explain?

when you have a chain like that, one of the statements will always execute, that means that you return a value at first iteration, effectively checking only first element of the array

check better, look that the greatest difference is not that the statements are nested, but that there is a return statement after the loop

1 Like