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?
<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 (var i=0; I<11; i++)
}
In that, I
is never defined, so you should probably change it to 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
a;
while(b){
d;
c;
}
and therefore to the do..while
loop of
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 );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.