Help me understand

Tell us what’s happening:
Can someone explain this please? As far as I understand:

  1. The for loop takes the length of the contacts array, because contacts is an array.
  2. The for loop adds 1 as long as the contacts array is greater than i, which is being added to every iteration.
  3. I don’t understand the first if statement.
  4. The second if statement checks if the certain array has the given property, I don’t understand how "contacts[i] works.
  5. Then it returns the contact and property two separate arrays.
  6. Else returns no prop if there isn’t a “prop”
  7. “no contact” returned if theirs no contact

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(firstName, prop){**
**  for (i = 0; i < contacts.length; i++) {**
**    if (firstName === contacts[i].firstName) {**
**        if (contacts[i].hasOwnProperty(prop)) {**
**          return contacts[i][prop];**
**      }**
**      else {return "No such property";}**
    }
  }
  return "No such contact";
}

// Change these values to test your function
lookUpProfile("Harry", "lastName");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/profile-lookup

The part that seems to be confusing for you is that contacts is an array of objects.

For example: in the for loop, contacts[0] is

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

That means that contacts[0].firstName is "Akira" and so on.

Does that help?

I understand that contacts is a array, what does contacts[i] mean?

I think you want to review how arrays work: Access Array Data with Indexes.
And maybe how loops work: Iterate with JavaScript For Loops.

I went over them again. All I need is an explanation of this line if (firstName === contacts[i].firstName)
Everything else makes sense.

In that line what is firstName? What is contacts[i]? What is contacts[i].firstName?

firstName is the name that is passed by you. contacts[i] is specifying an array within contacts. contacts[i].firstName looks for the firstName in the array.

Close. contacts[i] is not an array. It is an object. contacts is an array of objects, not an array of arrays.

Then why is it in brackets?

{} is the syntax for an object. [] is the syntax for an array.

I must sound like an idiot. The only thing I don’t understand is the contacts[i] part. I do not understand what it does. It’s not an array, yet it’s in []…so it’s an object. If I put contacts[0].firstName I would get Akira, that makes sense. Where does the [i] part come in, what does the [i] do? I appreciate you taking time to help a stranger on the internet, lol.

contacts is an array. contacts[i] is not an array. It is the ith element of contacts.

As you said in your original post:

i is created in the for loop and increased by 1 until the last index of the array.

Wow. And just like that it clicks. Thank you. So if I passed it “Sherlock” the loop would just repeat until it got to the 3rd element and found the firstName?

Exactly. :smiley: You got it.

1 Like

Nice, thanks for helping a stranger. :slight_smile:

My pleasure. It’s what I’m here for.
Happy coding.