12. Loops & arrays II

Ive been experimenting with the spacing and I keep getting the same error code.

error code…

Oops, try again. Your code should have printed ‘I know someone called Ava’, but didn’t. Make sure to type the 'I know someone called ’ part exactly right, and don’t forget the space between that phrase and the name!



var names = ["Ava", "Alex", "Becky", "Beatrice", "Cassandra"];

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


Replace the i above with 0 to target Ava!

Hope this helps! :smile:

With your code. the output in the console is:

I know someone called undefined

This is telling you that there is no value in names[i]. Your job then is to figure out “How could that be?” When you can’t spot an error yourself, ask your program to print out more information so that you can see what it is doing.

Try this:

    console.log("i= ", i);
    console.log("I know someone called"+" "+names[i]);

Does the output from that give you the clue that you need to spot the problem?

Let us know if that helps you with your troubleshooting.

Try spacing the +s.

console.log("I know someone called" + " " + names[i]);

I hope this helps!
If it does, click the “Solved” button below this reply.

I did the same thing as you,
have a look at the ; after your for (var i =0; i < names.length; i++);

remove that and you should be good to go :slight_smile:

You don’t need to put ( + " " ).

Try this:

console.log("I know someone called " + names[i] );

Ps - Make sure you give a space between called and the quotation marks (called ").

Putting
(+ " " + names[i]) does the exact same thing as doing

called " + names[i] :slight_smile:

Both create a single space before the name

1 Like

Look below where it says (HELLO). Delete the semicolon and run your code.

gl!

var names = [“Ava”, “Alex”, “Becky”, “Beatrice”, “Cassandra”];

for (var i = 0; i < names.length; i++); ---- (HELLO)
{
console.log(“I know someone called”+" "+names[i]);
}

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