Sample code output prints last line.as bangalore, 34 why?


#1

var friends = {
bill : {},
steve: {},
};

console.log(friends);

friends.bill.firstName = "Bill";
friends.bill.lastName = "Strong";
friends.bill.number = "345";
friends.bill.address = ["bang" , 34];

friends.steve.firstName = "Steve";
friends.steve.lastName = "Strong";
friends.steve.number= "345";
friends.steve.address = ["bangalore" , 34];

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


#2

Hi @ajit1709

Try to change it :

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

Hold on :wink:


#3

Thanks!

But, my doubt is in previous code on codeacademy console window... i see the below output..

{ bill: {}, steve: {} }
bill
steve
["bangalore",34]

which is surprising , what triggered the last 3 console lines... is it some problem or what?


#4

I think the codeAcademy console window is doing something more and that created confusion to me. Moved this code js file on local editor and could verify that nothing extra gets written on console (which was what I expected).

Thanks again to onepunch_man !