Tell us what’s happening:
I understand mostly how this is working, but only after looking up the solution - my own code didn’t even come close!
A couple questions:
- After the
for
loop, what does this part mean:
if (contacts[i].firstName === name)
I understand this as tying the for
loop into the nested if statement. This would allow the code to iterate through the contacts
array to begin checking each property against the name
provided in the function. Correct? While I still am unclear on the for
loops when used for recursion, I’m understanding that using a for
loop allows you to iterate through an array to check for a condition and that this will need to be tied into the executable(?) statement. Also correct?
- On this line of code:
return contacts[i][prop]
How does i
now stand in for the firstName property? I thought it would be:
return [firstName][prop]
- which did not work when I tried, lol.
What’s the difference? The console says firstName
not defined when I tried it my own way. I even tried putting firstName
in quotes and that didn’t work either.
I appreciate any clarification!!!
.
.
.
Your code so far
// Setup
const 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 (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
// Only change code above this line
}
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/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62
Challenge: Profile Lookup
Link to the challenge: