Reputation: 351
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>
Upvotes: 0
Views: 66
Reputation: 351
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.
Upvotes: 0
Reputation: 2610
try this,
function sub(x){
while(x > 100){
x = x-100
}
return x
}
document.getElementById("demo").innerHTML = sub(789);
Upvotes: 1
Reputation: 292
Using parenthesis in the while will fix your problem, just a small syntactical error
var x = 789
while (x > 100) {
x = x-100
}
Upvotes: 1
Reputation: 7188
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>
Upvotes: 4