Dont understand some codes


#1

I dont understand how some lines of code work, please see below (at the bottom):

thank you!!!


var friends = new Object();
friends.steve = 
{
    firstName: "Steve",
    lastName: "Smith",
    number: "666-666-666",
    address: []
};
friends.bill = 
{
    firstName: "Bill",
    lastName: "Smith",
    number: "666-666-666",
    address: []
};
var list = function (friends) 
{
    for(var firstName in friends)
    {
        console.log(firstName);
    }        
}
var search = function (name)
{
    for(var firstName in friends)       
//I think it is to search firstName of friends across different 
//keys(e.g. *  friends.steve  * and *  friends.bill  * ). Am i right?
    {
        if(friends[firstName].firstName === name)     
//what is *  friends[firstName]  *?
//what is *  .firstName  *? what does *  .firstName  * refer to?
        {
            console.log(friends[firstName]);        
 //what is *  friends[firstName]  *? I tried to print it out but it shows error
            return friends[firstName];
        }
    }     
}


#2

yep, you are right. firstName will hold the objects bill and steve (and more object if they would be present in the friends object)

you can simply see this by adding an additional console.log:

 for(var firstName in friends)   
   console.log("firstName is: " + firstName)

if you see that firstName holds bill and steve objects, so firstName is not a very logic choice. prop (from property) would be a more logic choice since it holds the properties of friends.

then this line:

if(friends[firstName].firstName === name)

we just decided that we renamed firstName to prop so then we get:

if(friends[prop].firstName === name)

now it already makes more sense since we don't have firstName two times

Okay, so prop will hold the bill and steve object, so then .firstName will get you the value of the firstName property of bill and steve object


#3

thank you very much for your help :heart:


#4

i renamed all the firstName and it is ok!
I am wondering is it i can name the variable whatever i like in for in loops?
So property include firstName, lastName, number......?

var search = function (name){

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

}

search("Steve")


#5

the variable as you called it, is called the iterator, and since you define the iterator in the loop, you can name it anything it you like.

But giving it a logic name would make sense, otherwise you have firstName twice, which is just very confusing...


#6

thank you * 100!!! :heart:


#7

another question:

how come the ** console.log ** and ** return ** can give Steve's property and value only instead of all?

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

search("Steve")

search("Steve") gives these:
{ firstName: 'Steve',
  lastName: 'Smith',
  number: '123',
  address: [ 'One Street', 'China' ] }
=> { firstName: 'Steve',
  lastName: 'Smith',
  number: '123',
  address: [ 'One Street', 'China' ] }

#8

i don't understand the question?

console.log(friends[property]);

this will give all information about property, which is in this case steve so that results in:

{ firstName: 'Steve',
  lastName: 'Smith',
  number: '123',
  address: [ 'One Street', 'China' ] }

and what is returned is very likely also displayed to the console

Bit tricky to tell, the exercise also calls your functions for validation purposes which can make it confusing when what is logged.


#9

my question is that the two lines of code are the same, why gives out different result?

one give out only Steve's while the other one gives out both's

input:

var search = function(name){
  for (var property in friends){
    if(friends[property].firstName === name){
      console.log(friends[property]);  // this line is the same as the example below
    }
  }
}

search("Steve")


------------------------------------------
output:

{ firstName: 'Steve',
  lastName: 'Smith',
  number: '123',
  address: [ 'One Street', 'China' ] }

input:

for (var property in friends){
  console.log(friends[property])    // this line is the same as the example above
}

------------------------------------------

output:

{ firstName: 'Steve',
  lastName: 'Smith',
  number: '123',
  address: [ 'One Street', 'China' ] }
{ firstName: 'Bill',
  lastName: 'Chan',
  number: '456',
  address: [ 'Two Street', 'China' ] }

#10

because of this if condition:

if(friends[property].firstName === name)

only if this is true, log the friend to the console. name is the function parameter, which has Steve as value:

search("Steve")

that is why search only list steve, and not steve and bill

kind of the point of the search function, to search for one friend and not list all friends like the list function


#11

thank you :heart: :heart:

i think i understand now


#12

Hi one more question, this exercise requires us to loop the property's value as a whole.

How about looping the character inside the value of a property, for example: i want it to loop the value "B" "i" "l" "l" inside the property:

friends.bill.firstName

is there any way to do it, coz i try the for in loop but doesnt work :frowning:

thanks


#13

A post was split to a new topic: 19. length


#14

the for in loop is for objects. "Bill" is a string, you could use an normal for loop:

var friends = new Object();
friends.steve = 
{
    firstName: "Steve",
    lastName: "Smith",
    number: "666-666-666",
    address: []
};
for (var i = 0; i < friends.steve.firstName.length; i++){
  console.log(friends.steve.firstName[i])
}

#15

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