Hello,
I’m trying to create code so when a user presses a button, X’s and O’s appear.
The x’s and o’s should work in a pattern of x, xo, xox up to 16 x’s and o’s.
I have created the button and I would like to use a for loop and an if, else statement.
I kind of understand how the for loop works but how would I use the if… else statement to do this?
Please see code in a reply below!
Thanks so much! Any help would be really really great.
<!DOCTYPE html>
<html>
<body>
<p> </p>
<button onclick="myFunction()">Generate pattern</button>
<p id="demo"></p>
<script>
function myFunction() {
var text = "";
var i;
for (i = 0; i < 10; i++) {
continue;
}
text += "X" + i + "<br>";
}
document.getElementById ("demo").innerHTML = text;
}
</script>
</body>
</html>
I think you’re misusing the continue statement here. It makes the computer jump to the next iteration of the loop without doing anything else. Because you have continue as the first statement of your loop, it makes your loop do nothing but count to ten.
Try this…
for (i = 0; i < 10; i++) {
text += "X" + i + "<br>";
}