Just a clarification of a concept


#1

https://www.codecademy.com/en/courses/building-an-address-book/0/5?curriculum_id=506324b3a7dffd00020bf661

All right it's not that my code is not working here, it's fine. But I just want to make sure that I understand this code well.

function search(lastName){
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
    if(contacts[i].lastName === lastName){
printPerson(contacts[i]);}

Alright, so the one part I want to get some clarification on is the "if" statement line. Is it just looping each of the last name properties of the different objects in the array and checking to make sure that it's unanimous to the function argument through the triple equal sign? Then, if it is unanimous, it prints the name of the person. Sorry if this seems a bit basic, I really want to make sure that I understand this correctly!


#2

The for is what actually loops through the array. The if line checks to see if the lastName the for loop is on is the same lastName that was passed in as the argument for the search function. if the lastName the for loop is on === the lastName that was passed in for the argument, then it executes the print line. So you've pretty much got it down. Hope this helps a little!


#3

yep, you got it right. It is comparing the lastname in the objects with the lastname parameter of your function. So now you can call the function to find different people:

search("Gates")

for example


#4

Alright sweet, I think I mainly had it down from the start, but I'm happy to see that I was thinking about this right.


#5

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