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^^
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");