Let's take a look at my code snippet:
<button id="submit">Roll</button>
<script>
function generateRandomNumber(range) {
var result = '';
var characters = '123456789';
var charactersLength = characters.length;
for ( var i = 0; i < range; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
var previousNumber = 0;
$('#submit').click(function(){
var isWinner = false;
var currentNumber = generateRandomNumber(3);
if(currentNumber > previousNumber){
isWinner = true;
}
previousNumber = currentNumber;
alert(isWinner);
})
</script>
The issue I'm encountering is that the winner for each round is always true, as the variable previousNumber
remains at 0.
I actually intend to compare the highest and lowest numbers across rounds.