I feel like I missed something. The solutions provided for the Profile Lookup challenge use .hasOwnProperty() and the “in” operator.
Did we learn either of these? I honestly don’t remember seeing either one in a lesson. Can anyone refer me to the FCC chapter that covered these?
I’m trying to create a solution based on what I’ve learned, and it’s not working but I’m not sure why. Can anyone take a look?
function lookUpProfile(name, prop){
// Only change code below this line
for (i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i][prop] === prop) {
return contacts[i][prop];
}
else {
return "No such property";
}
}
else {
return "No such contact";
}
}
// Only change code above this line
}
Not sure about in, it’s def covered but maybe later on. The hints contain a few possible solutions, sometimes they may use parts of the language you haven’t been taught yet
What happens if the first contact in the list doesn’t have a matching name?
Thanks for the quick response. If the first contact in the list doesn’t have a matching name, it should continue going through the for loop… right? And then I thought that if it doesn’t come across a matching name, it should refer to: else {return “No such contact”}.
Thanks for your help, everyone. I’m starting to understand this but am still hitting a mental roadblock.
In the past, I feel like I’ve used if/else statements in a for loop, but in this case the “else” doesn’t work - we have to put “No such contact” outside of the loop entirely. Is that only because the else in this case would involve a return (that ends the execution)? If I put in, say, console.log or print, would it continue to loop?