Loop the Loop with objects (Debugg Needed)


#1


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

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

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


#2

Shouldn't there be something inside the parenthesis?

A simplier way to do this is to replace the keyword Array with the array brackets[ ] .

Like the question before you want to use your Person constructor above by writing new Person before the name and age.

You want i to loop until it reaches family.length rather than 4.


#3

There is an error with my syntax:
SyntaxError: Unexpected token ')'. Expected a reference on the left hand side of an enumeration statement.


#4

I've fixed everything else I think.


#5

I think it just doesn't like the ")" in: "for (i=0, i<family.length, i++; )"


#7

Here's my code so far:
// Our Person constructor
function Person(name, age) {
this.name = name;
this.age = age;
}

// Now we can make an array of people
var family = []();
family[0] = new Person("alace", 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]);
};


#8

Just needed to pull out the commas and use semicolons instead.


#9

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

// Now we can make an array of people
var family = Array();
family[0] = new Person("alace", 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);
}


#10

Great job you got it in the end!


#11

#12

Oops. I thought it would just post the link.


#13

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