Profile Lookup: Accessing an array's object's properties

Profile Lookup: Accessing an array's object's properties
0.0 0

#1

Tell us what’s happening:
I tried to return the value of object’s property but… Could anyone look at my code and tell me what’s wrong with it?

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(name === contacts[i]["firstName"]){
      for(var property1 in contacts){
        if(prop === property1){
          return contacts[property1];
        }
        else return "No such property";
      }
      
    }
    else{
        return "No such contact";
      }
  }
// Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#2
 for(var i = 0; i < contacts.length; i++){
    if(name === contacts[i]["firstName"]){
      for(var property1 in contacts[i]){
        if(prop === property1){            
          return contacts[i][property1];
        }
        else return "No such property";
      }
      
    }
    else{
        return "No such contact";
      }
  }

It start with i===0 and if name === contacts[0]['firstName'] results true it proceeds, if it’s not true return 'No such contacts' ( it doesn’t check for i===1, 1===2 and so on).
There’s the same problem with the inner for loop

Hint:

‘Else’ trigger every time ‘If’ condition is not verified. In a situation like

for (let i=0;  i<3; i++) {
 if (i===x){
return 'lost'
}
return 'win'
}

you check all the three potential values for i, otherwise the code proceeds and it return ‘win’