Sandeep Mederametla
Sandeep Mederametla

Reputation: 1939

Why doesen't prompt() inside a for loop in javascript work for my code below?

Why doesen't prompt() inside a for loop in javascript work for my code below?

var P = [];
for(i=0;i++;i<10)
{
    var g=parseInt(prompt("What is the money you paid in"+i+ "month?"));
    P[i]=g;
}

Upvotes: 0

Views: 378

Answers (3)

David G
David G

Reputation: 96810

Your loop is formatted incorrectly, a for loop should be:

for ( state; condition; action )

So, given your case, the correct loop is:

for (var i = 0; i < 10; i++)

Upvotes: 1

pimvdb
pimvdb

Reputation: 154828

You swapped the parts of the for loop. The condition is second:

for(var i = 0; i < 10; i++) {

Also don't forget var, and parseInt(x, 10) prevents some weird behaviour.

Upvotes: 1

JJ.
JJ.

Reputation: 5475

Your for loop is wrong. It should be

for (i=0;i<10;i++)

You mixed up the second and third parts. The condition comes second, the variable increment comes last.

Upvotes: 3

Related Questions