Loop the Loop


#1



https://www.codecademy.com/en/courses/spencer-sandbox/4/2


My code looks right, the error message is unhelpful...


// Our Person constructor
function Person(name, age) {
    this.name = name;
    this.age = age;
}

// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

// loop through our new array
for (i = 0, i < family.length, i++) {
      console.log(family[i].name);   
}


#2

Hello @philhanson,

The aim of this exercise is for you to use JavaScript's for ... in loop. So modify your code so that it iterates over the Array by using a for variable in array loop and you'll be good to go!


#3

I'm sorry, I don't know what you mean...

Edit: You meant change my , to ; in the for loop I take it.


#4

Okay, @philhanson. This is the syntax of a typical for...in loop

var arr =  new Array();
arr[0] = "Allison"
arr[1] = "Bob"
arr[2] = "Charlie"

//Here comes the for...in
for (member in arr) {
   console.log(family[member0)
}

The above loop will log to the console: "Allison", "Bob" and "Charlie". You need to modify your code so that it behaves in a similar way. Hope this clears out your problems.


#5

Changing the , to ; in the for loop made it work.


#6

Oh ok... That's not the way I made it work, but I'm glad that it worked none-the-less. Keep coding hard :laughing:!


#7

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