Yuki
Yuki

Reputation: 13

JS square from 1 - 10

Hi I need to add to the table square fr 1 to 10 and I don't know what's wrong with my code (I'm new to JS)

Thanks for help

var kwad =[];

a = 1;


for (a>0; 10 === a; a++){
 kwad[a] = Math.pow(a,2) ;
 }

Upvotes: 0

Views: 149

Answers (1)

Furman
Furman

Reputation: 2395

In JavaScript for loop, each iteration will happen as long as second condition evaluates to true. In your case that never happens, because 10 === a always equals false (because a equals 1, so 1 === 10 will give you false). You should fix your code in following way:

const kwad =[];


for (let a = 0; a < 10; a++){
 kwad[a] = Math.pow(a,2) ;
}

Ps. Besides all, first statement in for loop is initialization, so code a > 0 doesn't really makes sense.

Upvotes: 4

Related Questions