Search for a friend - I do not understand these functions


#1

Please refer to the screenshot below, I have followed as instructed, looked at the initial prep code to arrive at making this lesson run, but I really do not understand the "FUNCTION" here, the "FOR/IN" loop, the "IF" statement, the "LIST" and "SEARCH" as seen in the last two lines below and why I have to use a random variable. Please, I would love to know how these works. Thanks


#2

i think you understand most, just not the for in loop

the for in loop does actually most of the work for you, it assigns (in order) the property in the friends object (bill and steve) to the variable you defined (guys in your case)

we could make a comparison to a array:

friends = ['bill','steve'];
for (i = 0; i < friends.length; i++){
  guys = friends[i]
  console.log(guys);
}

i used where possible the same variable names. but in the case of for in loop, the loop is most of the work for you, it detects the length, and assign property's of the friend object to guys. (or whatever you name this variable)


#3

I understand now @stetim94 Thanks a lot. Now if you take a closer look at my code, you will notice that in line 16, the "list" function, I have used "friend"....without "S" and my code still runs. Also in 21, the "search" function, I have used "name" here. I really do not know the function of these. "friend" and "name". They haven't been declared anywhere as a variable or in an object. Could you help me explain these?


#4

they are the function parameters? when you have a function:

var example = function(parameter){
   console.log(parameter);
}

then you can pass an argument when calling the function:

example("function parametrs aren't something new")

so in case of your list function you could do:

list(friends);

now friend (the function parameter) will hold the friends object. This should have been covered several tracks ago?


#5

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