Profile Lookup - pls let me know what went wrong!

Profile Lookup - pls let me know what went wrong!
0

#1

Tell us what’s happening:
i really dont understand what went wrong! please help !

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){
// Only change code below this line
  for(var i=0;i< contacts.length;i++){
if(contacts[i].firstName !== firstName){
  return "No such contact";
} else if (contacts.hasOwnProperty(prop) !== prop){
  return "No such property";
} else {
  return contacts[i][prop];

}
  }
// 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 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0.

Link to the challenge:


#2

Hi,

This is the same thing i got stuck on.
In this case what happens is the function just checks for the first array object which is wrong.
We have to allow our function to check through all the elements.
You can check the conditions below:

function lookUpProfile(firstName, prop){
// Only change code below this line
var isFirstnameFound=false;
var isProperty=false;
for(var i=0;i<contacts.length;i++)
{
if(firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop))
{
return contacts[i][prop];

  }
if(firstName == contacts[i].firstName)
  {
     isFirstnameFound = true;
  }

if(contacts[i].hasOwnProperty(prop))
{
isProperty = true;
}

}
if(!isFirstnameFound)
return “No such contact”;
if(!isProperty)
return “No such property”;

}


#3
if(contacts[i].firstName !== firstName){
  // when the first contact is not equal the firstName.. 
  // you returned it immediately. while you should check the second contacts.
  return "No such contact"; // return too early here.
}

everytime a program run time sees the return statement. it will leave the function regardless the for loop has been completed or not.

consider the example below

function someFunc() {
 let x = 0
 return x
 x = 1
 return x
}

// if i call 
let n = someFunc()

// will n becomes 0 or 1 ?