Profile Lookup Challenge - am I understanding this right?

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:

  1. 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?

  1. 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:

Hi @nurseoquin !

Contacts is an array of objects.
If we want to access an element in an array we use bracket notation and the index number.

For example, if I want to access the first object in the contacts array then I would write this

contacts[0]

That would return this first object

{
  firstName: "Akira",
  lastName: "Laine",
  number: "0543236543",
  likes: ["Pizza", "Coding", "Brownie Points"],
}

We use a for loop, because we need to check each object in our contacts array.
Then for loop will go from contacts[0] then contacts[1], etc.

contacts[i].firstName tells the computer to check each object in the contacts array and look for the firstName property.

This line of code

says, if the firstName in that object matches the name passed as an argument from the function call.

For this function call, lookUpProfile("Akira", "likes"); we are looking for the name “Akira” in these objects.
And we can see that “Akira” is a name found in the first object here

{
  firstName: "Akira",
  lastName: "Laine",
  number: "0543236543",
  likes: ["Pizza", "Coding", "Brownie Points"],
},

Yes.

I am not sure what you mean here.

contacts[i] represents the object.

In this function call

lookUpProfile("Akira", "likes");

We can rewrite the code like this for better understanding because the first object in the array is true for both if statements.

if (contacts[0].firstName ==="Akira") {
  if (contacts[0].hasOwnProperty("likes")) {
    return contacts[0]["likes"]; // this returns the value of the "likes" property which is an array [ 'Pizza', 'Coding', 'Brownie Points' ]
  }

Hope all of that makes sense!

1 Like

Yes, you break this down so much easier for me to understand - thank you!

That other part that I think I’m understanding was using a for loop - after setting up the conditions then you need to give the for loop something to do. I said executable statement, but I don’t think that’s an actual term, lol. :rofl:

Your formatting’s a little off, it should look more like this:

for (let i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name) {
    // stuff
  }
}

The if statement is in the for loop, not after it. (By the way, you can right click on the editor and select “format document”)

Thank you for this! The feedback has been so helpful, especially learning how to use the forum better, lol.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.