Hello! I got “stuck” kind of with the profile lookup challenge. I amended it to where I passed all of the tests, but I was curious about my original answer. Below is the given array:
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"]
}
];
Here is the answer that I initially gave. This worked for every test, except when a name did exist but the property did not exist.
function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++){
if(name === contacts[i]["firstName"]){
if(prop === "number" || "likes" || "lastName"){
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
// Only change code above this line
}
When I checked with a name that existed but a property that didn’t exist, nothing returned. I eventually separated
if(prop === "number" || "likes" || "lastName){
return contacts[i][prop];
} else {
return "No such property";
}
into
if(prop === "number"){
return contacts[i][prop];
} else if(prop === "likes"){
return contacts[i][prop];
} else if(prop === "lastName"){
return contacts[i][prop];
} else {
return "No such property";
}
And it worked. Is there a reason why the first section wouldn’t work with the || operator? Can you not use || with ===?
Thanks!