50
loading...
This website collects cookies to deliver better user experience
randomNum()
would return a random number, which could in turn do something like grab a specific item from an array (at the index of the random number returned). This allows you to create a basic lottery style system within an application.Math.random()
), the answer is well, not so random. It's technically pseudo-random. I'm not going to delve deep into the mechanics behind that but if you're curious I would highly recommend this article by Daniel Simmons.<div class="cont">
<h2 id="number">0</h2>
<button class="btn" id="generate">Random Number</button>
</div>
body {
background-color: #00242e;
}
.cont {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 100px;
}
.btn {
background-color: #32edd7;
border: none;
padding: 16px 32px;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
.btn:hover {
background-color: #2ad1bd;
}
#number {
font-size: 28px;
color: pink;
}
num
and btn
and assign them to the correct node.const num = document.getElementById('number');
const btn = document.getElementById('generate');
.random
method on the Math object. const randomNum = () => {
return Math.floor(Math.random() * 1000);
};
btn.addEventListener('click', () => {
});
num
with a random number, as produced by the randomNum
function.btn.addEventListener('click', () => {
num.innerHTML = randomNum();
});
num.innerHTML
and not num
. Otherwise, we'll be overwriting the num
variable and not updating the actual number visible on the page.