Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

310
Views
How to randomize the number before stopping at the lucky number. (Javascript)

I have a button that generates a random number (from 1-100) after a click. I have to make it:

  • Loop through random numbers with an interval of 100ms.
  • Loop stops after 10 numbers.
  • Disable the button when numbers are looping.
  • Enable the button when looping stops.

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);};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Loop through random numbers with an interval of 100ms.
  • Loop stops after 10 numbers. clearInterval(interval);
  • Disable the button when numbers are looping. btn.disabled = true;
  • Enable the button when looping stops. 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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!