#6 i got it right but did i really do it the way it wanted me to?


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

so this is my code. I am unsure if i did what it wanted me to do or if i just made up a way to do it. I am not very clear about what a for/in loop really does or how it works which leads me to be confused about the way i wrote my code is actually the correct way to do. any input would be of great help.


Hi @textrockstar76199,

Here's a simple for-in loop:

var object = {
    a: 1,
    b: 2,
    c: 3

for(var key in object){
    console.log(key, object[key]);  // key and its value


a 1
b 2
c 3

Try it on repl.it

So, all the for-in loop does is that it gives you one key of the object per iteration, and then you can do object[key] to get the corresponding value. Can you now figure out how to do the exercise correctly?

Hope it helps! :slightly_smiling:


^ thank you for the simple example. Question: how do we know that the var key, or ('object' in @textrockstar's question) will iterate over the keys of the object? Is this built in within the for/in structure, that no matter what you name it, the 'x' in syntax " for (var x in object)" will always refer to the key -- is that why you can name it anything you like?

Secondly, the the first part of the instructions stated that the function should take one parameter, but I found that the code works with or without it taking a parameter -- is this because there is only one object in the code so far?



That's how the for-in loop works! It will iterate over all the keys of the object!

There are some complexities like having enumerable property of values false or having modified prototype but these are way far and out of context.

I can't help much unless you share the full code.

Hope it helps! :smiley: