12 Loops and Arrays


#1

var names = ["mike","luthor","malcolm","martin","michelle"]
for(var i = 0; i<names[i]; i++){
    console.log("I know someone called" + " " + names[i]);
    }

Oops, try again. Your code should have printed 'I know someone called mike', 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!


#3

var names = ["mike","luthor","malcolm","martin","michelle"]
for(var i = 0; i < names[i]; i++){
console.log("I know someone called" + " " + names[i]);
}


#4

http://www.pythontutor.com/visualize.html#mode=edit
Maybe run your code there to see what it does. Short hint think about what this statement means and if it is what you wan it to be: i<names[i]

Spoiler

For 0 it would mean 0 < names[0] which would be 0 < "mike" which would not make much sense. Rather think about the length of names.


#5

Thanks I figured it out


#6

And the issue was?

Final result?

Improve the convo here, give the people what they need: info before they pull their hair out.


#7

i < names[i] is incorrect.
Its not the proper way to get the compiler to print the array.

i < names.length