Help me understand

Help me understand
0.0 0

#1

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:


#2

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?


#3

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


#4

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


#5

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


#6

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


#7

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.


#8

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


#9

Then why is it in brackets?


#10

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


#11

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.


#12

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.


#13

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?


#14

Exactly. :smiley: You got it.


#15

Nice, thanks for helping a stranger. :slight_smile:


#16

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