0

I want to start with a large number in a variable, and while it is larger than another number, subtract 100 from the variable. I then want the loop to put the variable in the "demo" paragraph, which should end up being 89. Any help is appreciated.

var x = 789
while x > 100 {
  x = x-100
}
document.getElementById("demo").innerHTML = x;
<p id="demo"></p>
Ele
  • 33,468
  • 7
  • 37
  • 75
Bryson Noble
  • 337
  • 1
  • 11

4 Answers4

4

Your while condition must be wrapped in parentheses.

var x = 789
while (x > 100) {
  x = x-100
}
document.getElementById("demo").innerHTML = x;
<p id="demo"></p>
UncleDave
  • 6,872
  • 1
  • 26
  • 44
1

Using parenthesis in the while will fix your problem, just a small syntactical error

var x = 789
while (x > 100) {
  x = x-100
}
Chizzele
  • 292
  • 2
  • 10
1

try this,

function sub(x){
while(x > 100){
x = x-100
}
return x
}
document.getElementById("demo").innerHTML = sub(789);
Aditya Thakur
  • 2,562
  • 2
  • 10
  • 21
0

Uhm - ok - I am really really really rusty at programming, but... you want to generate a predetermined static number and place that static number in a static html element programmatically? WTF man?

<p id="demo">89</p>

Is your solution.

No Javascript req'd. Unless you were hoping for a 'countdown' effect which: 1) Wont work with this code. 2) Wont work with corrected code. 3) Would work with a different methodology so fast you wont see it (you would have to introduce 'sleep' timers and so on).

It's been a bad day please don't take a picture.

Beeblbrox
  • 351
  • 1
  • 11
  • 1
    In my full code, the variable "x" will not always be the same number, the number 789 was just an example of what it might be. – Bryson Noble Apr 30 '19 at 20:23
  • My bad twas a bad mood post. Uh - you have considered simply https://stackoverflow.com/questions/48371657/how-to-remove-the-most-significant-digit-of-an-int-e-g-from-547-to-47-to-7?rq=1 from what I can see of what you are trying to do I hope? er - x % 100 would do instead of the while loop? – Beeblbrox May 02 '19 at 00:27