Tell us what’s happening:
Hello there, I’m trying to use an alternative solution for JS exercise “Profile lookup” as shown below without using the hasOwnProperty(). For some reason my code is skipping the first If statement even though the input values “Akira” and “likes” should both return the correct boolean to make the && true. Please see my code comments before and if any has ideas as to the flaws in my logic/code would really appreciate it.
Thank you
SL
Your code so far
//Setup
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"]
}
];
function lookUpProfile(name, prop){
for (var col = 0; col < contacts.length; col++){ //Create a for loop that tests every object from 0 - 3 (so all 4 objects)
if (contacts[col].firstName === name && contacts[col].prop !== undefined){ //Locate value at the object index of [col] and if that is true and also the property is not undefined move to return the property??
return contacts[col].prop;
} else if(contacts[col].firstName !== name) { return "No such contact";} //If the name doesn't match
else { return "No such property";}
}
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup