27 Loop The Loop


#1

Haha, this is the my second post on here in the last couple hours! Sorry people, my brain is not working today and I am very tired. But it would be fantastic if some kind gentleman could fix this common "Problem with your syntax" error I have here as I cannot get my head around it.

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

#2

close the Person class with this} at the end
like this

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

#3

@rydan is correct. You are missing the brace. Additionally, please format your post as I have done (select entire code block; press Ctrl+Shift+C) from next time. Thanks! :smile:


#4

i think you're also missing the "var" in the for loop. you wrote: for (i = 0; i < 4; i++). it should be: for ( var i = 0; i <4; i++).
I'm also a beginner.


#5

@hottcode Yes you are right! I skipped that. But it won't raise any error and the code will execute just fine. Variables declared without var are declared as global variables. However, it will give you an error in strict mode in ES5 as well as it isn't a best practice so declaring variables without var is strictly not advisable.


#6

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


#7

7 posts were split to a new topic: Cannot find error for missing semi-colon


#14

Hello, your condition in your for loop could be done with for-in, like this:

for (var i in family){
console.log(family[i]);
}
I think, it is easier.


#15

@coldfusionazet_sk

thanks for posting this, i read the hint for the lesson and did not follow the explanation so I ended up making the code i wrote worse. Also I did not think to write family.length which makes much more sense that restricting the code of the loop to a specific number of iterations as it frees up the code so it can continue looping as and when the family array list of members increases. Definitely not cut out to program as I make too many errors. At least I can go onto to complete the last unit now and perhaps study something that I don't find to difficult to learn


#16

3 posts were split to a new topic: How to print out only the names in lesson 27?


#18

// 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 x=0; x console.log(family[x].name);
};

It's work great!


#19

i don't know what JavaScript compiler or system you are using but that code has syntax error and will not work at all
check here

Three ways to correct it


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

OR

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

Using the Array.prototype.forEach function

family.forEach(function(member){
                console.log(member.name)
            });