12. Loops and arrays II - need help, can't find the error


#1

my code :

var names= ["Dandelion ","Ciri " ," Yennifer" ,"Geralt ","Triss "];
for ( var i=0 ; i< names.length ; i ++ )
{
console.log ("I know someone called " +" "+ names[i]);
}

It prints out nothing, whereas when I put a semicolon like this
for ( var i=0 ; i< names.length ; i ++ );
{
console.log ("I know someone called " +" "+ names[i]);
}
it prints only the first name, like this : I know someone called Dandelion

Do you see where my error is?


#2

That semi-colon separates the loop header from the body, leaving a block exposed as inline code. That is why it prints once.

Your first example outputs,

I know someone called  Dandelion 
I know someone called  Ciri 
I know someone called   Yennifer
I know someone called  Geralt 
I know someone called  Triss

Aside, regarding white space...

for ( var i=0 ; i< names.length ; i ++ )
     ^       ^                 ^   ^  ^

All the spaces indicated could be removed to make your code more conventional.

i=0
 ^^
i< names.length
 ^

The indicated spots could use a space to make the operator more visible.

Eg.

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

#3

thanks ! but it stills print nothing when I erase the semicolon;

it may be a bug?


#4

I ran the code in the lab and it worked fine, as indicated above. Please post a link to the lesson to we can test it there. Thanks.


#5

https://www.codecademy.com/courses/javascript-beginner-en-NhsaT/2/4?curriculum_id=506324b3a7dffd00020bf661

you're welcome


#6

As suspected there is extra white space in your strings.

var names= ["Dandelion", "Ciri", "Yennifer", "Geralt", "Triss"];

and there is an extra space after, called that should be removed.

"I know someone called" + " " + ...

#7

hi,

I've removed all extra spaces but it stills print absolutely nothing on the console...

even an error message ...


#8

In fact, I've just reset the code and write it down again, and it works.
Was kind of a bug, thanks for the help anyway !


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.