for (let i = 0; i < contacts.length; i++){
if (name == contacts[i].firstName){
if (contacts[i].hasOwnProperty(prop)){
console.log(contacts[i][prop]);
return contacts[i][prop];
} else {
return "No such property";
}
} else {
return "No such contact";
}
}
It is basic code solution. It works
for (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === name) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
No, is not a mistake but you can see that your loop is not going further and your function returns to early.
Never gets console.log(name == contacts[i].firstName) is true (for the failure case).
(console.log(name == contacts[i].firstName) should return true eventually because the name parameter is indeed the same as contact[3].firstName but your loop never gets there, for lookUpProfile(“Kristian”, “lastName”)) example)
Basically you want to review the if else logic from your code.
Does that help?
Quick fix:
function lookUpProfile(name, prop){
// Only change code below this line
for (let i = 0; i < contacts.length; i++){
if (name == contacts[i].firstName){
if (contacts[i].hasOwnProperty(prop)){
console.log(contacts[i][prop]);
return contacts[i][prop];
} else {
return "No such property";
}
}// else {
// return "No such contact";
//}
}
return "No such contact";
// Only change code above this line
}
The solution is to remove the return ‘No such contact’ from the loop body so you prevent the iteration being stopped too early.