Profile lookup - returning No such contact when it should not

I have written this:

for( var i=0; i<contacts.length; i++){
     if(contacts[i] === name){

       if(contacts[i].hasOwnProperty(prop)){

        return contacts[i][prop];

      } return "No such property";  

      } return "No such contact";
      }
    }

and the page gives me as feedback that “No such contact” comes out if inserted a name not existing. I do not understand if it returns it because it’s correct or because I made such a mess that it’s just a coincidence.

Reason why I should move it outside the for, isn’t it? I did it, but nothing has changed.

//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] === name){

       if(contacts[i].hasOwnProperty(prop)){

        return contacts[i][prop];

      } return "No such property";  

      } 
      }return "No such contact";
// Only change code above this line
}

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

I am still stuck with this problem, does anybody know how to help me?

This is not doing what you think it is
(contacts[i] === name)

contacts[i] is a contact object, name is a string

Can someone explain to me why one single loop is enough?

I thought it only goes through the amount of elements in the first array which in this example would be 4 objects in the array.

I would think you need a nested for loop to run another one just like in the “Nesting FOR loops” exercise with the multiplyAll function

for(var i=0; i < contacts.length; i++)
    {for (var j=0; j < contacts[i].length; j++)

You can’t use a loop like that to search through the properties of an object, but you also don’t need to iterate through the properties of the object

You know that there is a firstName property (contacts[i].firstName) and you have an other name of a property that you have to check (contacts[i][prop])

You can do everything with that

1 Like