rinran
April 30, 2018, 6:53pm
1
Tell us what’s happening:
Can someone explain this please? As far as I understand:
The for loop takes the length of the contacts array, because contacts is an array.
The for loop adds 1 as long as the contacts array is greater than i, which is being added to every iteration.
I don’t understand the first if statement.
The second if statement checks if the certain array has the given property, I don’t understand how "contacts[i] works.
Then it returns the contact and property two separate arrays.
Else returns no prop if there isn’t a “prop”
“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?
rinran
April 30, 2018, 7:35pm
3
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 .
rinran
April 30, 2018, 7:56pm
5
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
?
rinran
April 30, 2018, 8:05pm
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.
Close. contacts[i]
is not an array. It is an object. contacts
is an array of objects, not an array of arrays.
rinran
April 30, 2018, 8:09pm
9
Then why is it in brackets?
{}
is the syntax for an object. []
is the syntax for an array.
rinran
April 30, 2018, 8:17pm
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.
contacts
is an array. contacts[i]
is not an array. It is the i
th element of contacts
.
As you said in your original post:
rinran:
The for loop adds 1 as long as the contacts array is greater than i, which is being added to every iteration.
i
is created in the for
loop and increased by 1
until the last index of the array.
rinran
April 30, 2018, 8:23pm
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?
rinran
April 30, 2018, 8:31pm
15
Nice, thanks for helping a stranger.
My pleasure. It’s what I’m here for.
Happy coding.