Profile Lookup , array navigation problem

Profile Lookup , array navigation problem
0.0 0

#1

Tell us what’s happening:

So Basically I have coded what i think its the right thing. But… It doesn´t work, so I checked the console.log, and I notice that I always get returned the “firstname” of the [0] position. Seems that it never goes up.

Akira
VM6966:35 Akira
VM6966:35 Akira
3VM6966:35 Akira

And that makes no sense , since it should be

Akira
Harry
Sherlock
Kristian

So that makes me being stuck, cause array isnt being navigated. Pretty weird
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++){
      console.log(contacts[i].firstName);
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)){
      return contacts[i][prop];
    }else if (contacts[i].firstName != name){
      return "No such contact";
    }else if (contacts[i].firstName == name && !contacts[i].hasOwnProperty(prop)){
      return "No such property";
    }
  }



}

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

If I delette the whole code, and simply put this

for (var i=0 ; i<contacts.length;i++){
console.log(contacts[i].firstName);}

It will return:

Akira
VM7200:46 Harry
VM7200:46 Sherlock
VM7200:46 Kristian
VM7200:46 Akira
VM7200:46 Harry
VM7200:46 Sherlock
VM7200:46 Kristian
VM7200:46 Akira
VM7200:46 Harry
VM7200:46 Sherlock
VM7200:46 Kristian
VM7200:46 Akira
VM7200:46 Harry
VM7200:46 Sherlock
VM7200:46 Kristian
VM7200:46 Akira
VM7200:46 Harry
VM7200:46 Sherlock
VM7200:46 Kristian
VM7200:46 Akira
VM7200:46 Harry
VM7200:46 Sherlock
VM7200:46 Kristian

Why is that happening? Shouldnt it return each name one time?
Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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


#2

The problem is that you always return when i is 0. Remember that a return statement ends function execution. In your code, if the first entry is the correct name and has the property, you return the property value (nothing else happens). If it is not the correct name, “No such contact” is returned (nothing else happens). If it is the correct name and the property does not exist, it returns “No such property” (nothing else happens). There is no case where the next item in the array would be checked.


#3

No, the function is called several times to see if all tests pass.