27.loop the loop


#1


Here is what I'm stuck at:
https://www.codecademy.com/en/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661#


I'm getting a syntax error, Im not sure why...


// Our Person constructor
funtion 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);
}


#2
have a close look at

funtion


#3

make sure that you typed Person and not person.


#4

Haha, always something so small! Great catch. Also OP, this helped me out so thanks!


#5

Oh...Hahhahaha! Yeah...that typo....Thanks rcodeman


#6

I typed Person. as rcodeman said: the problem was funtion instead of function


#7

// 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] = Person("alice", 40);
family[1] = Person("bob", 42);
family[2] = Person("michelle", 8);
family[3] = Person("timmy", 6);
// loop through our new array
for(var i =0; i < family.length; i++) {
console.log(family[i].name);
};
whats wrong? it show family[i] is undefined!


#8

family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

It should be like this.


#9

There was a problem with your syntax. Please, help me!

// 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();
var family[0] = new Person("alice", 40);
var family[1] = new Person("bob", 42);
var family[2] = new Person("michelle", 8);
var family[3] = new Person("timmy", 6);
// loop through our new array
for(var i=0; i<family.length; i++)
{
console.log(family[i].name);
};


#10

I am having the same issue SyntaxError: Unexpected token

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

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);

for(var i=0; i<family.length; i++) {
console.log(family[i].name);
};


#11

I am pretty sure my code was okay. still dont know why it shows family[i] is undefined.


#12

Hi, The following is my code:

// 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();

// loop through our 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);

for (i=0;i<=family.length;i++)
{
console.log(family[i].name);
};

I am getting the type error message "Cannot read property 'name' of undefined".

Could someone please help. Thanks.


#13

Please ignore, have figured out what the problem is.
I replaced <= with <. It now works.


#14

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