27 Loop the Loop


#1

Hi guys,

I get this error: SyntaxError: Unexpected token '='. Expected an opening '(' before a function's parameter list.

My code:

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

// Now we can make an array of people
var familiy = 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);

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

Help please :smile:


#2

Problem is in this code:

var i === 0

Just think about this for a minute. Does it make sense?

Here you should assign value to the new variable, but you use equality check operator (===). You have to use = operator.


#3

I changed the code to var i = 0 and it still doesn't work, what am I missing here?


#4

I found the problem: "var familiy = new Array();"

I wrote familiy instead of family.

Thanks :smile:


#5

Sorry, missed this one :smile:


Side note, your constructor doesn't look right:

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

It should be:

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

or:

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

#6

you could use for loop as below also

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


#7

Please give me answer

need help a.s.a.p finale last assignments


#8

The solution is:

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

// Now we can make an array of people
var familiy = 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("Our family contains " + family[i].name);
}