Javascript | For loop - What is my syntax error?
Javascript/JQuery
var gallerynr = 3;
var thumbsnrA = new Array(gallerynr);
var a = 0;
var b = 0;
var d = 0;
for (d = 0; d == gallerynr; d++) {
thumbsnrA[a] = 22;
a ++;
b ++;
};
alert(thumbsnrA);
Result (should be):
22,22,22
But my Result is:
,,
What is wrong with the syntax of m开发者_JAVA技巧y code?
You test is wrong.
for (d = 0; d < gallerynr; d++) {
is what you want
If you have
for (d = 0; d == gallerynr; d++) {
it only runs when d is 3 and it is not 3 from the beginning
Your array variable is declared as thumbsnr
, but you reference thumbsnrA
later on in the code.
Edit - OK, so you've changed your code! There is a misplaced semi-colon after your loop - that could be throwing the error.
You don't need a delimiter after the closing brace of the for loop, and if d!=gallerynr before the loop, then the for loop will never be entered.
精彩评论