Tuan Do Huu
Tuan Do Huu

Reputation: 29

counting program with javascript

I have a homework:

import 3 numbers n,m,k. Count all numbers between m and n divisible by k

And this is my code, can anyone tell me what is wrong with that?

var n = parseInt(prompt("enter N"));
var m = parseInt(prompt("enter M"));
var k = parseInt(prompt("enter K"));

for (var i=0; n<=i<=m; i++)
{
    if (i % k == 0)
    {
        document.write( i + '&nbsp;');
    }
}

Upvotes: 0

Views: 130

Answers (1)

Nino Filiu
Nino Filiu

Reputation: 18473

The for loop has to be rewritten as such:

var n = parseInt(prompt("enter N"));
var m = parseInt(prompt("enter M"));
var k = parseInt(prompt("enter K"));
for(var i=n; i<=m; i++){
	if (i % k == 0){
		document.write( i + '&nbsp;');
	}
}

Upvotes: 1

Related Questions