For / In Loop on "List 'em all" won't search for bill


#1

Could someone check this code to see if there's anything wrong? When I search for bill nothing comes out. (Oh yeah, can someone else also explain how to insert code so that it's already formatted correctly?)


var friends = new Object();

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

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
}
};

var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(123) 456-789",
address: ['Waverly Street', 'Palo Alto', 'CA', '98052']
}
};

var list = function () {
for (var k in bill) {
console.log(bill[k])
}
};


#2

There is no object, bill, only friends.bill. Instead your function should be referring to the friends object:

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

Looking back up through the code we see,

var friends

declared three times. Each time, wiping out the one previous. Only declare a varable once, and not again.


var friends = { bill: {}, steve: {} };
Populate the two secondary objects with the data you have above.