Exercise 6 please help


#1

var friends = new Object();
friends.bill={
firstName : '',
lastName : '',
number :'',
address:[]
};
friends.steve={
firstName : '',
lastName : '',
number :'',
address:[]
};
friends.sneha={
firstName : '',
lastName : '',
number :'',
address:[]
};
friends.aparna={
firstName : '',
lastName : '',
number :'',
address:[]
}
friends.prakkam={
firstName : '',
lastName : '',
number :'',
address:[]
};
var list = function(friends)
{
for(var key in friends)
{
console.log(key);
}
}

actually my code went through but I did not understand anything..could some one explain me for loop part and what is that "key" element in the code


#2

It should explain it very well in the bar on the left side of the screen. In the for loop that you created, you are setting a temporary variable named 'key' that is set to one item in the dictionary 'friends' every time it is run. When you print the temporary variable each time during the loop, the variable is set to the key from the list until there are no more keys. You don't even have to use the variable 'keys'. You could even say something like:

for( var i in friends){
    console.log(key)
}

#3

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