Search for a friend?


#1



https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#

My code works but I don't understand the logic behind it.


var friends = {};
friends.bill ={
    firstName:'Bill',
    lastName:'Gates',
    number:'555-555-5555',
    address:['One Microsoft Way','Redmond', 'WA', '98052']
    };
friends.steve={
    firstName:'Steve',
    lastName:'Jobs',
    number:'777-777-7777',
    address:['Apple Headquarters','Scilicon Valley', 'CA', '78642']
    };
friends.kenli={
    firstName:'Kenli',
    lastName:'Schoolland',
    number:'808-885-5555',
    address:['The World','Everywhere', 'Planet Earth', '010101']
    };
friends.caroline={
    firstName:'Caroline',
    lastName:'Robbins',
    number:'409-782-8816',
    address:['Rich Area','Houston', 'TX', '76557']
    };
    
var list = function(object) {    
    for (var person in object) {
        console.log(person);
    }
  }; 
  
 var search = function(name) {
     for(var first in friends) {
         if(friends[first].firstName===name) {
             console.log(friends[first]);
             return friends[first];
         }
     }
 };

I don't understand:

  1. How does this code below call on the friends object that I made above it? I never called on the object friends so how does it know that that is the list I am talking about? In the code below what does object represent and what does person represent?

var list = function(object) {
for (var person in object) {
console.log(person);
}
};

  1. Almost the same question for this following piece of code. When did I ever call on the object friends with this code? what does first represent and what does friends reference. And when I use [first] like this friends[first] is that an index or what is that doing exactly. I am super confused as to how the last pieces of code function with the object I made with all my friends information.

var search = function(name) {
for(var first in friends) {
if(friends[first].firstName===name) {
console.log(friends[first]);
return friends[first];
}

Thank for the help!


#2

When you call it using list(friends), it understands that you want it to use the friends object.

object is the object that you called the function list with, and person represents every value in the object, each in turn.

In the bold. for(var first in friends) {

Look up a for/in loop.

It is looking for the value stored to the key that is the same as the value of first.


#3

Hello! Thank you for taking the time to answer my questions.

But you stated some code that I didn't use. For example I literally put

var list = function(object) {
for (var person in object) {
console.log(person);
}
};

And I asked how that called on the friends object. You said list(friends). I get that but I never typed that in my code yet it told me that the original code that I mentioned in my first post was correct. No where in my code does it say list[friends] but yet it still worked and I don't understand why my code worked.


#4

The code in a function is not executed when the function is defined. It is executed when the function is invoked.
some people use the term "call a function" instead of "invoke a function".
so here list function is not working
call the function you'll understand it.

var list = function(object) {
for (var person in object) {
console.log(person);
}
};
list(friends);