27 Loop the Loop - what am I doing wrong?


#1

Hi guys,
I've been struggling with this one for a while, but can't figure out what the problem is.
It keeps giving the following error:
"Oops, try again. The following error was thrown when trying to test the family array: TypeError: Expecting a function in instanceof check, but got 3"

// 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 (var i = 0; i <= 3; i++) {
console.log("Hello, " + family[i].age + "!");
};

Any help is welcome, thanks!


#2

Hi here

for (var i = 0; i <= 3; i++)

Instead of <= 3 put the length the array family like that

for (var i = 0; i < family.length ; i++)

#3

It works now, thanks!


#4

// 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 (var i = 0; i < family.length; i++) {
console.log(family[i].name);
};

Can someone please tell me what I am doing wrong?


#5

Nvm I think there was just a glitch with the website. It works now :pensive: