-2

I want to check For EXAMPLE:

12 - 13 - 14 - 15

12 / 3 = 4 -> OK.

13 / 3 = 4.33 -> NOT OK.

14 / 3 = 4.67 -> NOT OK.

15 / 3 = 5 -> OK.

I want to create a while loop where i use the number i want to divide to 3 as "x".

so:

var x = 0
while (x<20) {
//SOMETHING HERE
x++;
}

Something like that but i dont know the command to check if the sum is a decimal number or not.

Than i want to see the "OK. numbers" in my web browser with a documetn.write( OK numbers )

Liam
  • 27,717
  • 28
  • 128
  • 190
  • 3
    [modulo](http://en.wikipedia.org/wiki/Modulo_operation) – desbo Jan 27 '14 at 17:15
  • how to use modulo? can you give me an example code for javascript? –  Jan 27 '14 at 17:16
  • 3
    [Find if variable is divisible by 2](http://stackoverflow.com/questions/2821006/find-if-variable-is-divisible-by-2) This is 2, but the same applies for 3 – Liam Jan 27 '14 at 17:17

1 Answers1

3

You can use the modulo operator to check if a number is divisible by another.

Demo

var x = 0;
while (x < 20) {
    if (x % 3 === 0) {
        document.write(x + ' ');
    }
    x++;
}

Also, the W3C recommends against using document.write now. Instead it is better to use document.createElement to create an element and insert it that way, like this:

Demo

var span = document.createElement('span');
span.innerHTML = text;
document.body.appendChild(span);
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
  • Than how can i use each x variable in a addition as: `x1 + x2 +... + xEND` and `document.write(sum of all the x)` –  Jan 27 '14 at 17:23