Tell us what’s happening:
I tried doing the prop search in this exercise two ways 1. a nested for-loop(commented out) 2. hasOwnProperty method. The name check goes through and returns correctly if the name is not found, but the inner check for prop is not catching. I can’t see the bug. It seems like it should work.
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){
// Only change code below this line
for (var i = 0; i < contacts.length; i++){
if (contacts[i][0] == name){
// for (var j = 0; j < contacts[i].length; j++){
// if (contacts[i][j] == prop){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
else
return "No such property";
}
else
return "No such contact";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "address");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup