27 loop the loop


#1

I'm stuck here, and can't figure out what's wrong with my code. can anyone help me with this ?
thanks in advance!
// Our Person constructor
function = Person (name, age){
this.name = name;
this.age = age;
}
var family = new Array();

// Now we can make an array of people
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

Hi here

function = Person (name, age)

I think that it should be

Person  = function(name, age)

#3

Are you getting a TypeError, too? I don't understand what I did wrong, either; my code looks almost exactly like yours. :grin: The exact error is > TypeError: Cannot read property 'name' of undefined

`// 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("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(i = 0; i<=family.length; i++) {
    console.log("Hello, my name is " + family[i].name + "!");
};`

#4

IT WORKED !
Your solution did work and even better it pointed out what I was doing wrong;
function = Person (name, age)
instead of
function Person (name, age)
Thanks a lot !!:smiley:


#5

mine was an "Unexpected token ="
as you see, the answer was there but I didn't pay attention :expressionless:
for you, try to create the array like this :
var family = new Array();


#6

OMG I didn't see your error, either! :grin: I guess this is a perfect example of how you can become blind to the errors you're looking for by staring at the same code for hours. LOL! :grin:


#7

OK, I've got it! It doesn't matter if you use var family = new Array(); or var family = [] as long as you DON'T USE i<=family.length in your for loop. I didn't think it would matter if you used <= or < in your loops. In fact, I thought it was safer to use <= because I knew it would stop on the exact last element. I thought that if I just used <, it would loop backwards infinitely. :grin: Guess I was wrong.


#8

An array of length 3 has the indexes 0 1 2, using an index equal to the length would be outside the array. The loop should therefore start at 0 and continue for as long as the current index is less than the length.


#9

This was my issue too!


#10

Ah, so for(var i = 0; i<arr.length; i++) is the same as saying for(var i = 0; i<=arr.length-1; i++) because i<=arr.length stops at the actual number of items in the array, whereas i<arr.length stops at the last index of the array. Am I right?


#11

You can test that.

arr = [1, 2, 3];
for (var i = 0; i < arr.length; i++) {
    console.log('arr[' + i + '] = ' + arr[i]);
}

#12

help please, console writes what it should be the right answer, but it keeps sayin` some error...


#13

Hi you should creat e you person begin from 0 to ... like that

people[0] = ...
people[1] = ....
people[2] = ....
.......

So you for loop will be like that

Fo (var i = 0;.....)

#14

This works for me...
// Our Person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}
var family = new Array();
// Now we can make an array of people

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{
console.log(family[i].name,family[i].age);
}


#15

// Check this one.. its working to 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();
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);
};


#16

dude.. where is ur for loop.. gets messup..:relaxed:


#18

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


#19

I'm new as well, but I am anxious to help! While reviewing your lines, trying to correct my own, I did happen to see that you haven't declared you (i) as a var inside your loop. Or outside for that matter! Hope that helps! Happy Coding!


#20

You are getting "undefined" though because of your (<=). Change it to (<).


#21

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

this solves issue. if you put <= then it throws undefined in console, remove = it