Profile LookUp in basic javascript

Please help me out from here … what is the bug I can not find out.


function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0;i<contacts.length;i++){
  if(name == contacts[i].firstName){
     if(contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
  }
  else{
      return "No such property";
  }
  }
  else{
    return "No such contacts";
  }
 
}
// Only change code above this line
}

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

If (name == contacts[0].firstName) means that you’re checking if the name argument is equal to contacts[0].firstName( in the first round of the loop). Which is fine.
Problem is, what if it is not equal? You put in an else statement that trigger every time the if statement is not true. Which is not good.
If the name doesn’t correspond to the first element of contacts array it trigger the else, returning "No such contact" and you’re done before to even check the other elements of the array^^

oh shit it is still not working … I am blind now.


function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0;i<contacts.length;i++){
  if(name === contacts[i].firstName){
     if(contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
  }
  else if(contacts[i].hasOwnProperty(prop)==false){
      return "No such property";
  }
  }
  else if(name != contacts[i].firstName){
    return "No such contacts";
  }
 
}
// Only change code above this line
}

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

Ah, sorry my friend but even if the syntax is changed the logic is not :frowning:

Try to involve in your thoughts the for.. loop - You should check all the contacts. After, if you had no results, you can return "No such contact"

ok . would you like to suggest me an editor ,where I can see my output .Here I can not see my output

You can click ctrl + shift + I to open the browser console. After that, under the ‘console’ tab, you can see the output of console.log.

So, you can do something like
console.log(lookUpProfile("Akira", "likes")) and see the results in your console ^^

hey my friend, I can not work with this. it is showing :Capture

Ah, I didn’t explain clearly. Here is a screenshot:

My result is undefined since I didn’t write any function, anyway you will see the result and almost all the error when they occurs ^^