Loops and Arrays 2 confusion


#1

Code:
// Click on "Stuck? Get a hint!" if you get stuck!

var names = ["Person1", "Person2", "Person3", "Person4","Jokes on you, this isn't a name!"];

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

Console:
"I know someone called Joe Dirt"

What am I doing wrong?


#2

This line has 2 problems..
for (var i = 0; i > names.length; i++);
A) the > should be <
B) delete the ; at the end


#3

@applesauce10189,

You are using a semi-colon in the wrong position.
A semi-colon is an End-of-Statement indicator....

for ([initialization]; [condition]; [final-expression]) {
   statement
}

You also have to change your condition
from your

i > names.length

or

i < names.length

#4

@mr_sommers You sir, are a god. Thank you. Also, the semi-colon at the end was just me messing around trying to figure out if maybe my syntax was off, putting that semi-colon there made the first line of text appear so I figured I was on the right track,


#5

HELP!?


#6

@microace01843,

Include the names[i] into your console.log()-Methods-ARGUMENT
like

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

#7

Hi, Lenohard, thank you for the feedback, it was very useful.

I had the same problem with the semicolon and now it's solved and I can continue, but I wouldn't like to do so without understanding it clearly.

What was exactly the problem with the semicolon? When I ran the program the only thing appearing was "I know someone called undefined". Why was it undefined, if from the beginning i = 0 and so at least the first name should have appeared? What was the role of the semicolon in that?

Thanks


#8

@tonygaln,
The console.log() Method
expects a so-called ARGUMENT

In Javascript you can create a =concatenated-String= with
"I know someone called " + names[i]
and use that as an argument
console.log("I know someone called " + names[i])

In Javascript you use the semi-colon-; as an End-of-Statement indicator
thus
console.log("I know someone called " + names[i]);

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide


#9

Thx loads. That ■■■■ ";" haha. Other than that, everything else was dope.