0

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;');
    }
}

4
  • 5
    document.write is very very rarely used in modern javascript. You would do best to forget that it even exists Commented Nov 4, 2018 at 15:09
  • I think that you have to change for loop because n is <= of i only when is negative or 0. Commented Nov 4, 2018 at 15:34
  • 1
    what wrong with that – please describe how have you figured something's wrong with your solution and what exactly is wrong. Questions demanding help with debugging have to contain the expected and the actual behaviours (as well as the shortest code which reproduces the issue – you have that already) Commented Nov 4, 2018 at 20:18
  • 1
    You list the numbers that fulfill the predicate instead of counting them. Commented Nov 4, 2018 at 20:18

1 Answer 1

1

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;');
	}
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.