Tell us what’s happening:
I can get the solution to work using the standard answer. Now I wanted to test for the invalid items first so I rewrote the code.
pseudocode
for each X in array
Check for name; if not found return “no name found”
Check if the property isn’t valid; return “not a valid prop”
else
return prop and name
What am I missing? It is like it isn’t progressing through the loop or something?!
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){
console.log("First Step")
for (var counter = 0; counter < contacts.length; counter++) {
console.log(contacts[counter]["firstName"]);
if (name != contacts[counter]["firstName"]) {
return "No such contact";
}
console.log("step two")
if (contacts[counter].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
// Change these values to test your function
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 12105.53.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.61 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup