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


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?


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< names.length

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


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


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

it may be a bug?


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.



you're welcome


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" + " " + ...



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

even an error message ...


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 !


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