I have a button that generates a random number (from 1-100) after a click. I have to make it:
Not sure if I have done the code correctly, I am able to let numbers loop after click but unable to make it stop after 10 numbers. I am also unable to diable when looping and enable the button when looping stops. Appreciate your kind help. Thanks!
Here's my code:
btn.onclick= function (){
var random = Math.floor(Math.random()*100+1);
var interval = setInterval(function(){
random = Math.floor(Math.random()*100+1);
document.getElementById("lucky").innerHTML = "Your Lucky Number is: " + random;},100);};
clearInterval(interval);
btn.disabled = true;
btn.disabled = false;
var btn = document.getElementById('randomize');
btn.addEventListener('click', function () {
btn.disabled = true;
var cpt = 0;
var interval = setInterval(function() {
cpt++;
var random = Math.floor(Math.random()*100+1);
document.getElementById("lucky").innerHTML = "Your Lucky Number is: " + random;
if (cpt === 10) {
btn.disabled = false;
clearInterval(interval);
}
},100);
});
<button id="randomize">click me</button>
<div id="lucky"></div>