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

I keep getting this error message:
We're running a test below to make sure your code works.

TypeError: Cannot read property 'name' of undefined

not sure what is wrong?

Thanks


#2

You're missing the ; after this.name = name and this.age = age.


#3

No that didn't help, it's still giving the error.


#4

The problem is the semicolon here:


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

Have a look at this for an explanation:


It's about conditions but applies for loops as well and if you have further questions feel free to ask.


#5

Dudes...thats the correct 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] = 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);
};


#6

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

}
:wink:


#7

Hate to tell you but actually it's not. Here your for loop is entirely useless and just this statement is executed:

console.log(family);

Have a look at the link in my post, it's about conditions but this applies to loops as well.


#8

ok but why the page tells my that everything is ok?


#9

Good question probably a bug in the SCT (test function) but you're for loop is definetly not used (at least not to console.log the familiy members one by one).


#10

Sorry but what is the correct Code now?


#11

What @betasolver15481 wrote:

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

The problem with your code is as said the semicolon to wrap this up. The {} after conditions and loops are actually not needed (wait before you delete them all) but in case you omit them the loops and conditions only work on the very next statement. So the {} wrap up more statement to act like one. In your case this next statement is a semicolon and you can think of a semicolon as the shortest command ever: /*nothing to do here*/;
And that is whats happening your loop runs and counts up till it reaches it's condition but it does nothign at all.


#12

Thanks you really helped me out.


#13

how did you solve the problem please fill me in


#14

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("name: "+family[i].name+" age:"+family[i].age+"\n" );

}

this code runs,and this is the output by screen...

name: alice age:40

name: bob age:42

name: michelle age:8

name: timmy age:6


We're running a test below to make sure your code works. (<-- and this does not go away)
name: alice age:40name: bob age:42name: michelle age:8name: timmy age:6