简体   繁体   English

Javascript循环未运行或未结束

[英]Javascript loop either not running or not ending

I want to start with a large number in a variable, and while it is larger than another number, subtract 100 from the variable. 我想从变量中的一个大数字开始,虽然它大于另一个数字,但从变量中减去100。 I then want the loop to put the variable in the "demo" paragraph, which should end up being 89. Any help is appreciated. 然后,我希望循环将变量放在“演示”段中,该段最终应为89。感谢您的帮助。

 var x = 789 while x > 100 { x = x-100 } document.getElementById("demo").innerHTML = x; 
 <p id="demo"></p> 

Your while condition must be wrapped in parentheses. 您的while条件必须用括号括起来。

 var x = 789 while (x > 100) { x = x-100 } document.getElementById("demo").innerHTML = x; 
 <p id="demo"></p> 

Using parenthesis in the while will fix your problem, just a small syntactical error 在一段时间内使用括号可以解决您的问题,只是一个小的语法错误

var x = 789
while (x > 100) {
  x = x-100
}

try this, 尝试这个,

function sub(x){
while(x > 100){
x = x-100
}
return x
}
document.getElementById("demo").innerHTML = sub(789);

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? 嗯-好的-我在编程时真的很生锈,但是...您想生成一个预定的静态数并将该静态数以编程方式放置在静态html元素中? WTF man? WTF男人?

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

Is your solution. 是您的解决方案。

No Javascript req'd. 不需要Javascript。 Unless you were hoping for a 'countdown' effect which: 1) Wont work with this code. 除非您希望获得“倒数”效果,否则:1)不能使用此代码。 2) Wont work with corrected code. 2)无法使用正确的代码。 3) Would work with a different methodology so fast you wont see it (you would have to introduce 'sleep' timers and so on). 3)可以以不同的方法工作得如此之快,以至于您看不到它(您必须引入“睡眠”计时器等)。

It's been a bad day please don't take a picture. 今天真是糟糕的一天,请不要拍照。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM