1

I got to make script that should generate random number from 1 to 10 on element click and every next click that variable should be higher than previous generated number.

There is my code, I just need to add that "adding next random number to previous generated random number". I'm stuck there a little bit :-)

$(document).ready(function () {

  $('#wheel-start').click(function () {
    var min = 1;
    var max = 10;
    var totalDegree = Math.floor(Math.random() * (max - min + 1)) + min;

    $('#wheel-rotate').css({
      'transform': 'rotate(' + totalDegree * 72 + 'deg)'
    });

    console.log('totalDegree: ' + totalDegree);
  });
});

Thanks in advance!

4
  • What happens if the first number is 9 or 10? Commented Aug 1, 2019 at 13:29
  • Doesn't matter what the first number is. Commented Aug 1, 2019 at 13:30
  • "and every next click it should be higher than previous" Commented Aug 1, 2019 at 13:31
  • Edited the description. Commented Aug 1, 2019 at 13:33

3 Answers 3

1

Just update the min with the newly generated number. But once it reaches the upper limit, you can't get any other number other than the max range.

$(document).ready(function () {
  $('#wheel-start').click((function(min, max) { 
       return function() {
          var totalDegree = Math.floor(Math.random() * (max - min + 1)) + min;
          min = totalDegree;
          max = min === max ? (max+10) : max;

          $('#wheel-rotate').css({
             'transform': 'rotate(' + totalDegree * 72 + 'deg)'
          });
          console.log('totalDegree: ' + totalDegree);
       }
    })(1, 10));
});
Sign up to request clarification or add additional context in comments.

8 Comments

min will be set as 1 on every click. Because of "var min = 1;" line. You have to move "var min = 1" to out of function scope.
Also, you need to update max as well, otherwise you'll only be generating that number.
@muratonnet Updated it to work correctly. Thanks for pointing it though.
@chepkasov Upgrade the limit to what? I will just update the code to upgrade the limit to another 10. You can modify it as per your requirement later.
Exactly what I need, thank you, you're the best! :-D
|
1

Just save totalDegree in a higher scope and increment its value each click:

function clickCallback(){
    var min = 1;
    var max = 10;
    var plus = Math.floor(Math.random() * (max - min + 1)) + min;
    totalDegree += plus;

}

Change min and max to define a smaller/bigger random step

Comments

-1
$(document).ready(function () {
  var n=0;

  $('#wheel-start').click(function () {
  var min = 1;
  var max = 10;
  var go=true;
  while(go){
     var totalDegree = Math.floor(Math.random() * (max - min + 1)) + min;
     if(n<totalDegree){
        n=totalDegree;
        go=false;
     }
  }

  $('#wheel-rotate').css({
    'transform': 'rotate(' + totalDegree * 72 + 'deg)'
  });

  console.log('totalDegree: ' + totalDegree);
  });
});

Have a nice day

2 Comments

This will turn into an infinite loop when n = 10
It's suffice add an "exception" to prevent it. My "solution" responds to the question and stop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.