Profile Lookup | Why has the "no such contact" part to be outside of the for-loop?

Profile Lookup | Why has the "no such contact" part to be outside of the for-loop?
0

#1

Tell us what’s happening:
Hi,

my code is working now - after I looked at the reference in the help section.
(So please don’t read further, I you didn’t solve the problem yet.)

But my question is with the structure of the code.
The last statement return "No such contact"; - why is it not moved one curly bracket up?

function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    }
    else {
      return "No such property";
    }
  } 
  else { // this is, where I'd place the else clause
 return "No such contact";
  }  
}
// Only change code above this line
}

Right now I can’t wrap my head around why it’s not working that way.
It seems to make sense that the first if statement (if the given parameter matches a name) the looking up goes on… but if not, then something else happens. And this else-statement should, as far as I understand if/else statements, be on the same level as the if-statement began.
But with the given working code, it is not on the same level - AND there is no else-statement. It’s tossed outside of the whole for-loop, and I just don’t get why.

And a related question: the working code, wouldn’t it return “no such contact” every time, even if the contact exists, just because it’s placed inside the function but outside of the loop?

Could someone walk me through, please? I guess I’m not seeing the wood for the trees right now.

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) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    }
    else {
      return "No such property";
    }
  } 
}
    
  return "No such contact";




// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/


#2

Move it up one curly bracket and add some console.log statements to see what the values are for various variables. For example, add a console.log(i) before the first if statement and see how far your code gets with a test case which should return “No such contact”.

Whenever you can not understand the flow of a working solution, put these types of checks in so you can see what is happening.

Also remember, that when a return statement is executed, the function exists and does not come back to finish up a loop.