6 List 'em all! Straight over my head


#1

I absolutely hate it when I flat out just don't get something. But truly, I don't. I have no idea how the for/in loop works, and how I'm supposed to incorporate it with my objects within objects mess of code above. Any help, explanation, or guidance beyond the poor half assed explanation given in the lesson would be great.

var friends = new Object();

friends.bill= new Object();
friends.steve= new Object();

friends.bill.firstName= "Bill";
friends.bill.lastName= "Gates";
friends.bill.number= 8889991111
friends.bill.address= ["One Microsoft Way", "Dickmond", "WA", "98052" ];
friends.steve.firstName="Steve";
friends.steve.lastName="Jobs";
friends.steve.number= 2223334444
friends.steve.address= ["Penisberg Drive", "Megalodouche", "CA", "77777"];

var list = function(bill){
for(var lookup in bill) {
console.log(friends)
}


#2

Okay I made a correction to my code:

var friends = new Object();

friends.bill= new Object();
friends.steve= new Object();

friends.bill.firstName= "Bill";
friends.bill.lastName= "Gates";
friends.bill.number= 8889991111
friends.bill.address= ["One Microsoft Way", "Dickmond", "WA", "98052" ];
friends.steve.firstName="Steve";
friends.steve.lastName="Jobs";
friends.steve.number= 2223334444
friends.steve.address= ["Penisberg Drive", "Megalodouche", "CA", "77777"];

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

and it passes me and prints this...

bill
steve
["Penisberg Drive","Megalodouche","CA","77777"]

I don't really get the result or how this even works. Like... Why did it print bill, then steve and then only steve's address? Where did all of bills info go? And where did the rest of steve's info go? The result just seems erratic at best.


#3

To be less confusing sounding, since you want to loop through each of your friends change that to:

for(var friend in friends) {

Similarly, instead of:

you can write:

console.log(friend);

Doesn't that sound more like what you are trying to do?


#4

You forgot the ; sign at the very last curly brace.


#5

Hi, was just wondering why in

"for(var friend in friends)"

Why is "friend" = key ? I cant see the word "friend" except for "friends" obviously, anywhere else in my code. So why do I start using it in my for loop ?

Sincerly

A noobie learner :slightly_smiling:


#6

You are creating that variable at that point, so you could call it anything.

for (item in friends)
for (thing in friends)
for (property in friends)

Those all work the same.

friend in friends just sounds right to me. "For each friend in the friends array"


#7

Ok, Thank you for your reply


#8

Hi, noobie learner here too. I don't get this one too. It's good that it let you pass with the for/in code. But just like the OP, I don't get where the rest of the information are. When you do a console.log(bill), shouldn't all the info under "bill" print out? and not just the last variable? why would "steve" even print out if it wasn't called out?

Can someone please enlighten me? or us? As I don't think OP's question was properly explained. And if it was, I do apologize. Just wanting to learn and understand this more. Thank you.


#9

When I put:

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

console.log(bill);

I get

bill
steve
ReferenceError: bill is not defined

The bill and steve are from console.log(friend);
You could avoid the ReferenceError with console.log(friends.bill) if you wanted to.


#10

Your code was "for(var friend in friends)". What you did in the for/in loop, is declare a variable called "friend". You won't see it anywhere else in your code because you just created / declared that variable when you typed "var friend".