[英]Javascript while and do/while loops
I have this code who works properly with for loop, how to made while and do/while loop to work properly on same way when i click on button? 我有这段代码可以正确使用for循环,当我单击按钮时,如何使while和do / while循环以相同的方式正常工作?
<body>
<div>
<button onclick = "fun()">Click
</button>
<p id = "dr">
</p>
<script>
function fun() {
var str = "";
for (var i = 0; i < 11; i++) {
str = str + i + "<br/>";
document.getElementById("dr").innerHTML = str;
}
}
</script>
</div>
</body>
This sounds a bit like HW, so I won't give you the entire code. 这听起来有点像硬件,所以我不会给您完整的代码。
However, you have this code in your for loop 但是,您的for循环中有此代码
for (var i=0; I<11; i++)
}
In that, I
is never defined, so you should probably change it to i
. 在那方面,
I
从未定义过,因此您可能应该将其更改为i
。
For changing it to a do{}..while()
loop, remember that every for loop of the form for(a;b;c){d;}
can be expanded to the while loop 要将其更改为
do{}..while()
循环,请记住for(a;b;c){d;}
形式的每个for循环都可以扩展为while循环
a;
while(b){
d;
c;
}
and therefore to the do..while
loop of 因此到的
do..while
循环
a;
do{
d;
c;
}while(b);
while: 而:
function fun()
{
var str ="";
while( i < 11 )
{
str=str + i +"<br/>";
document.getElementById("dr").innerHTML =srt;
i++;
}
}
do/while: 做/做:
function fun()
{
var str ="";
do
{
str=str + i +"<br/>";
document.getElementById("dr").innerHTML =srt;
i++;
}while( i < 11 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.