Hi everybody. I’ve tried to look a similar code to mine but I have not found anything, so I’ve created this topic:
This is the challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup.
My solution doesn’t work:
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
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].hasOwnProperty(prop)) {
return "No such property";
}
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
This is how the code works:
function lookUpProfile(name, prop) {
for (let 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";
}
Why my code doesn’t work with a && operator in a if statement but it works with nested if statements? Sorry if my doubt is so easy.