.6 List em - Please help with console log and for statement


#1

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

Oops, try again. It looks like your list function isn't printing the right output. Output only prints 2nd person and not the first person.

var friends = new Object();
  friends.bill = {
  firstName: "Bill",
  lastName: "Gates",
  number: "(206) 555-5555",
  address: ['100 Microsoft Way', 'Seattle', 'WA', '90852'],
};
var friends = new Object();
  friends.steve = {
  firstName: "Steve",
  lastName: "Jobs",
  number: "(207) 222-3303",
  address: ['1002 Apple Way', 'Seattle', 'WA', '90852'],
};
var list = function(friends){
  for ( var i = 0; i < friends.length; i++){
   console.log(friends)
  }
}


#2

You want to use a for in loop instead of the counting for loop. I suggest, for print in friends, console.log(print)
Just because instruction two says use a for/in loop.


#3

Thank you that worked.


#4

Awesome, no problem!


#5

@kyleaw is right a for in loop would work much better and efficiently but also you only need to call this line once,

var friends = new Object ();

remember as well to add a semi colon after the last bracket to end that statement.

 }
} //Here ;

for in loop syntax looks like this,

for (var x in example) {
    console.log(x)
}

#6

@scriptplayer48364, let's look to your code again:

 var friends = new Object(); // you created empty object friends using base Object constructor
  friends.bill = { // and put bill object into empty friends object using literal notation
  firstName: "Bill",
  lastName: "Gates",
  number: "(206) 555-5555",
  address: ['100 Microsoft Way', 'Seattle', 'WA', '90852'],
}; // now you have friends object which contains bill object

var friends = new Object(); // you created empty object friends using base Object constructor
  friends.steve = { // and put steve object into empty friends object using literal notation
  firstName: "Steve",
  lastName: "Jobs",
  number: "(207) 222-3303",
  address: ['1002 Apple Way', 'Seattle', 'WA', '90852'],
}; // now you have friends object which contains steve object

So, after that you have friends object which contains only steve object because friends object was recreated by using of base Object constructor - new Object().

Remember: new Object() creates new empty object, that's why you have only steve object in friends object.

Next.

var list = function(friends){
  for ( var i = 0; i < friends.length; i++){
   console.log(friends); // ; was missed
  }
}

You tried to use for loop for outputting friends properties. But for this goal is for...in statement:

The for...in statement iterates over the enumerable properties of an object, in arbitrary order.

Should be:

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

Result:
list(friends);
bill
steve

In your case you need to find friends length if you want to use for loop:

var list = function(friends){
  for ( var i = 0; i < Object.keys(friends).length; i++){
   console.log(friends);
  }
}

Result:
list(friends);
Object {bill: Object, steve: Object}
Object {bill: Object, steve: Object}

#7

any one can explain what is "for in loop"? in detail;

var list = function(obj) {
for(var prop in obj) {
console.log(prop);
}
};

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


#9

@sumitstark, this doc should help you:

JavaScript for/in Statement


#10

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