Tell us what’s happening:
What’s working so far: I am able to check if “name” is an actual contact’s first name. I am also able to return the prop for “lastName.”
However, the last name being returned is for the first object (0). I thought by nesting my loops, that the second loop would search within the one that corresponds with the “name” match, but it doesn’t. Do you have any tips/suggestions on how I can get the nested loop to work?
Please don’t tell me the answer and instead give me hints/suggestions.
Thank you!
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 (const test1 in contacts){
let value1 = contacts[test1]["firstName"];
if (name === value1){
for (const test2 in contacts){
let value2 = contacts[test2]["lastName"];
if (prop === "lastName"){return value2};
}
}
}
}
// Change these values to test your function
// lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.
Challenge: Profile Lookup
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup
Thanks all for the help!!! Works now. Much appreciated.