12. Loops & arrays II


#1

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]);
}

#2

Replace the i above with 0 to target Ava!

Hope this helps! :smile:


#3

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.


#4

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.


#5

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:


#6

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


#7

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

called " + names[i] :slight_smile:

Both create a single space before the name


#8

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]);
}


#9

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