+ 6
How can I get random numbers from this code?
This script is suppose to create a random number but it returns the same number everytime it is ran. var roll = 0; var dice = Math.floor(Math.random() * 10) + 1; loop: do { roll = prompt("Guess the magic number!"); if (roll == dice) { alert("You win! " + dice + " is the magic number."); }else if (roll === '' || isNaN(roll)) { alert ("Please enter a number"); }else if (roll === null) { alert ("Cancelling Game...Goodbye!") ; break; } else { alert("You lose! " + dice + " is the magic number."); } }while (roll !== 1);
2 Respostas
+ 12
This:
var dice = Math.floor(Math.random() * 10) + 1;
should be placed inside the loop.
Tested the fix on Code Playground and is working fine.
+ 2
yea explanation: you initialized dice to a random number, but in the loop you never changed it, so it was always the same number