27/33 Unexpected token [


#1

Can anyone help me find the mistake in this solution?

// 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] = Person("alice", 40),
family[1] = Person("bob", 42),
family[2] = Person("michelle", 8),
family[3] = Person("timmy", 6),
};

// loop through our new array
for(i=0; i console.log(family[i];, i++);


#2

Look closely at this:

What you should look for:
1) Semi colons
2) Commas (there's one hidden in there)
What you should make:
1)Two brackets that indicate the start and end of the for loop


#3

is this what you mean?
for
(
i=0; i console.log(family[i]; i++
);


#4

I was just looking and trying to find the level you were at, and I found it...

There's a lot of corrections.:neutral_face:
Let's do it step by step, shall we?

First:

supposed to be

var family = newArray();

Second: No brackets here:


Also, before those codes, you must put

var family = [];

Third, you have to state that "i" is a variable here:

var i = 0;
is the correct way.

Take out the semi colon & comma in your for loop that are bolded.

Finally,
this is what your for loop should look something like:

for (var i=0; i < family.length ; i++ ) {

AND TO WRITE THE INSIDE, THIS IS AN EASY WAY

console.log( "Family-member:" + family[i] );
console.log( "Family-member-name: " + family[i].name );
console.log( "Family-member-age : " + family[i].age );

MAKE SURE YOU ADD A BRACKET AT THE END

Hope it helped!:smiley:


#5

Let's put that in one line:

for (i=0; i console.log(family[i]; i++);

While you were correct to take out the comma the for loop works more like what you see below:

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

Don't get discouraged I'll go into detail for you!

I was going to go a bit further but I see @ragezapper has just posted and knocked everything I was about to describe out of the park! Pay attention to his post :smiley:

I'll explain the for loops though!
for loops consist of four main parts:

  • Initialization,
  • Condition,
  • Increment/Decrement "postfix".
  • Execution

for (initialization ; condition ; increment/decrement "postfix") {
  //Execution
}

Initializaiton: i = 0 ;
Condition: i < family.length ;
PostFix: i++
Execution: Your code!


Expected ')' error, need help figuring out whats wrong
#6

Thanks @ragezapper and @lolman for the help. It is really appreciated. My solution now looks like this

// 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();
var family = [];
family[0] = Person("alice", 40),
family[1] = Person("bob", 42),
family[2] = Person("michelle", 8),
family[3] = Person("timmy", 6),

// loop through our new array
for ( var i = 0; i < family.length; i++);
{
console.log(family[i]);
}

and I still recieve an error message that reads
SyntaxError: Unexpected token for


#7

family[0] = Person("alice", 40),
 family[1] = Person("bob", 42),
 family[2] = Person("michelle", 8),
 family[3] = Person("timmy", 6),

You're ending these lines with commas " , " when you need to end them with semi-colons " ; ". Hope it helps!

--EDIT--

The only reason @ragezapper got away with doing that is because he was implying that the family array would have everything initialized within the family array right off the bat.

var family[] = new Array{
 family[0] = Person("alice", 40),
 family[1] = Person("bob", 42),
 family[2] = Person("michelle", 8),
 family[3] = Person("timmy", 6),
};

@ragezapper, does my syntax look right with this?


#8

@lolman that makes sense.

I fixed it up and this time I received a unique error message that reads
: undefined


We're running a test below to make sure your code works.
undefined.

and also says

It looks like the object at index 0 was not created with the Person constructor.


#9

Did you add code to your for loop? It should look something like this:

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

#10

Yes I did but It did not do the trick for some reason


#11

My solution looks like this now

// 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] = Person("alice", 40);
family[1] = Person("bob", 42);
family[2] = Person("michelle", 8);
family[3] = Person("timmy", 6);
// loop through our new array
for ( var i = 0; i < family.length; i++);
{
console.log(family[i]);
}


#12

NO SEMI COLONS after a LOOP.

Look closely here:

See that semi colon? That's the problem with your code.


#13

in addition to what is said take a look at this

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

// Now we can make an array of people
var family = []; // you can still use new Array() notation

//Now here you have created a person class at the top
// to create a person object you have to use the new keyword
//like this new Person(args)
//because you are missing this thats why undefined is always
//printed to console
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]);
}

or

// if you wanted to have a formatted output rather than just
//the raw object representation
// you can try this for the looping though array part
for(var member = 0; member<family.length; member++){
    var out = "Family member " +(member+1)+": "+family[member].name+", "+family[member].age +" years";
    console.log(out);
}

You can test it here too


#14

Thank you! I was able to spot that one of the core issues was the semi colon that I placed after the for loop. Also thanks @ragezapper!