Tell us what’s happening:
I’ve been running this through pythontutor constantly with little adjustments and have figured out that I don’t know how to reference a key name rather than the value of the key in an object. For instance: In this exercise you have to see if prop
is equal to a key in one of the objects in the array, to do that, I’m thinking if (contacts[i].prop === prop)
should work. I’ve tried many other variations like contacts.i - contacts[i] - contacts[prop] etc. but no matter what I see the code will break at that point in the loop since it doesn’t return true any more.
In short: How do I reference the name of a key in an object that’s part of an array?
Also, I am aware that my if… else loop at the bottom doesn’t work properly, that’s something I’ll worry about later.
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){
// Start
for (var i = 0; i < contacts.length; i++) {
if (contacts[i]["firstName"] === name) {
if (contacts[i].prop === prop) {
return contacts[i][prop];
}
}
}
if (name !== contacts["firstName"]) {
return "No such contact";
} else if (prop !== contacts[i]) {
return "No such property"
}
// Stop
}
// Change these values to test your function
lookUpProfile("Kristian", "lastName");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup