Profile Lookup of contacts freecodecamp

Profile Lookup of contacts freecodecamp
0.0 0

#1

Tell us what’s happening:
i wrote this but it isnt fully working
i have read somewhere on here that the issue is not “running loop through index before returning” but idk what that means exactly …

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(firstName, prop){
  for(var i =0; i<contacts.length; i++){
    if(contacts[i].firstName == firstName){
      if(contacts[i].hasOwnProperty(prop)){

        return contacts[i][prop];

      }else{

        return "No such property";

      }
    
     }else{
       return "No such contact";  
             } 
}

  
      
    
     
      
    
 

// Only change code above this line


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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2

In order to be able to check each person object in the contacts array, you have to be able to look at each person in turn. Your code returns “No such Contact” before each person can be checked as it is inside the for loop.


#3

Remember that as soon as a return statement is reached, the function is done. Your code only checks the first item in the array because it always hits a return.