Hello all!
I’m pretty new to this and I need someone to help me understand what I’ve done wrong. I’ve created code on a web page that contains a ‘while statement’ and a ‘do/while statement’. When I click the ‘try button’ on the webpage only one will work. The while and do/while statement won’t work in the same document, but if I put the codes in separate documents they will work. I’d be grateful if someone could help as I’ve no idea what to do. I’ve pasted the code below.
Best wishes
Bob.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of code as long as i is less than 5.</p>
<button onclick=”myFunction()”>Try it</button>
<p id=”demo”></p>
<script>
function myFunction() {
var text = “”;
var i = 0;
while (i < 5) {
text += “<br>The number is ” + i;
i++;
}
document.getElementById(“demo”).innerHTML = text;
}
</script>
</body>
<body>
<p>Click the button to loop through a block of code as long as i is less than 5.</p>
<button onclick=”myFunction()”>Try it</button>
<p id=”demo”></p>
<script>
function myFunction() {
var text = “”
var i = 0;
do {
text += “<br>The number is ” + i;
i++;
}
while (i < 5);
document.getElementById(“demo”).innerHTML = text;
}
</script>
</body>
</html>