Profile Lookup - please review my code?

Tell us what’s happening:

Hey Campers … not sure what is wrong with my code … can anyone lend a clue, pls?

Thanks!!

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; contacts.length; i++) {
    if(contacts[i].firstName === name && (contacts[i].hasOwnProperty(prop))){
            return contacts[i][prop];
        } else if (contacts[i].hasOwnProperty(prop) === false) {     
        return "No such property"; } 
        else if (contacts[i].firstName !== name){
            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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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

Your code is not working because it is returning something at the first iteration and never checking the other users

Try following the flow of your code with this: http://pythontutor.com/javascript.html

Check this one!

https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/

The issue :

first you should know that when a for loop encounters a return statement, it stops, just like when it encounters a break statement.
in your code, your’e checking if a contact has the fisrstName and prop to return the wanted prop. That would work only in the case where the first element in contacts satisfies those constraints, otherwise the else branch that checks for the condition (contacts[i].firstName !== name) would get executed, effectively terminating the for loop and reaching an early conclusion if you will, without ever checking the other elements of contacts.

The solution

remove the last else if and return "no such contact" outside the for loop, that way you can be sure it checked every single element of contacts without finding a match, like so:

function lookUpProfile(name, prop) {
  for (var i = 0; contacts.length; i++) {
    // it has the  fistName and the prop
    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    }
    // it has the firstName but no prop
    else if (
      contacts[i].firstName === name &&
      contacts[i].hasOwnProperty(prop) === false
    ) {
      return "No such property";
    }
  }
  // exhausted all contacts without finding a match
  return "No such contact";
}