27. Loop the loop


#1

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

It says my syntax is wrong and i have been rusty on loops for a while , but the problem is i think my code is also wrong could somebody elaborate ?


#2

I am also stuck at this,

for(i = 0; i < family.length; i++) {
    return family[i].Person;
};

#4

Hi! in the for loop you have : var = i and it is supposed to be var i = 0. Now it should work.


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

{
        console.log(family[i].name);
}

I wrote this and it worked. Basically your family[i] work as your "this" in your method. At least that's how i think it works


#6

Wow thank you guys it did work . I had a hunch my loop would be bad but when i was trying to refresh myself i could not find the correct syntax for it , and for who is interested yes my method did work i did not change "family[i].name" to "family[i].Person"


#7

I used this and, it says I have no variable name, jeez, it is exactly the say WhatTheDuck Codecademy
`
// Our Person constructor
function Person(name, age){
this.age = age;
this.name = name;
};

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


#8

You typed in var(for... when it should be for(var...


#9

thanks, now I feel silly that that happened.:laughing:


#10

don't worry , believe me i know the feeling :smiley:


#11

I tried this and it comes up with an error message:
"Oops, try again. It looks like the object at index 0 was not created with the Person constructor"
the console reads :
"---
We're running a test below to make sure your code works.

TypeError: Cannot read property 'name' of undefined"


#12

Did you get this figured out cause I'm getting this message as well.


#13

Unless you require an offset, for loops should use: for (var key in family){ console.log(family[key].name)}


#14

CORRECT EXAMPLE:

// Our Person constructor
function Person(name, age){
this.name = name;
this.age = age;}
family = [];
// Now we can make an array of people
family.alice = new Person("alice",40);
family.bob = new Person("bob", 42);
family.michelle = new Person("michelle", 8);
family.timmy = new Person("timmy", 6);

// loop through our new array

for (var key in family){
console.log(family[key].name)}


#15

// 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("My name is " + family[i].name + "My age " + family[i].age);
}


#16

Hi! I am not understanding why i got such error: SyntaxError: Unexpected token [
this 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();
family.[0]=("alice",40);
family.[1]=("bob",42);
family.[2]=("michelle",8);
family.[3]=("timmy",6);
}

// loop through our new array

for (var i=0; i{
console.log("My name is"+family.[i].name + "and my age is"+family.[i].age);
};


#17

Sorry, I got. I forgot to write 'new Person' in Array.
Have to be very careful at every point....:sweat_smile:


#18

CORRECT SYNTAX

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


#19

Hey, can you explain to me why we write family[i].name what is the reason behind it, all rest of the code i've completly understood execpt this part! thanks!


#20

this also worked for me, I had for (var i = 0; and changed it to for (i = 0; and it worked


#21

why does
for (i = 0; i < family.length; i++)
work, but
for (var i = 0; i < family.length; i++)
does not?

throughout the course while learning about "for" loops, the latter is how we were told to write them.