Tell us what’s happening:
I did my code again and i wrote, but i checked it against the answer:
Looks like the answer uses a for loop and nests two if statements in the for loop. contacts[x] represents each object nested in the array. Then it uses dot notation to see if firstName === the name parameter. Then they use the Object.hasOwnProperty() method to check for the prop passed in to the parameter. if it has the name and property, then it returns them. Otherwise it returns ‘no such property’ and then it returns ‘no such contact’.
Your code so far
I thought my code would have been close or solved it
//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
/* loop through every contact with a for loop
-set a variable let contact=contacts[i];does <name> === contact[firstName]
does<prop>===contact[firstName][prop]
if they both equal, return them in an array
let contactArray =
contactArray.push(name, prop);*/
for (let i=0; i<contacts.length; i++){
let contact=contacts[i];
let contactArray = [];
if(name === contact[name] && prop === contact[firstName][prop]){
return contactArray.push(name, prop);
}
/* If name !== contact[firstName]{
return 'No such contact;*/
}if (name !== contact[firstName]){
return 'No such contact';
}if(prop!==contact[firstName][prop]){
return "no such property!"
}
/*If prop !== contact[firstName][prop]{
return "No such property!";*/
}
// Only change code above this line
// Change these values to test your function
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/