Profile lookup - returning No such contact when it should not

Profile lookup - returning No such contact when it should not
0

#1

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.


Basic JavaScript: Profile Lookup-Somequestion
#2

I moved your question to its own topic, because you were asking something about your own solution instead of answering the question posed by the original poster of the other thread.

In the future, just click the Ask for Help on the challenge to ask questions about your own solution issues.

Thank you.


#3

Remember that when a return statement executes, the function exits. This means, your for loop only makes one iteration before something is returned and the function exits.


#4

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


#5

I would need to see your full code to comment further. Make sure to post not just the for loop, but all the code.


#6
//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");

#7

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


#8

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

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


#9

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++)

#10

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