I am trying to generate 40 random numbers between 0 to 8
This is my code --
<body>
<h1>This is a page for randomize number between 0 - 8 </h1> <br />
<button onclick="randomize()">Random number</button>
<p id="Output"></p>
<script>
function randomize() {
var arr = []
while(arr.length < 40){
var randomnumber=Math.floor((Math.random() * 8) + 1)
arr.push(randomnumber)
}
document.getElementById("Output").innerHTML = arr;
}
</script>
</body>
I am expecting 40 random numbers to be Output like --
4 4 5 7 8 1 5 7
What I am doing wrong can someone correct me please and also I want the result to be outputted by the next line !
Can someone help !
Your code already works, just add line breaks:
function randomize() {
var arr = []
while(arr.length < 40){
var randomnumber=Math.floor((Math.random() * 9)) // 0-8 is 9 numbers
arr.push(randomnumber)
}
document.getElementById("Output").innerHTML = arr.join('<br>'); // Add line break between numbers
}
<h1>This is a page for randomize number between 0 - 8 </h1> <br />
<button onclick="randomize()">Random number</button>
<p id="Output"></p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With