I have a loop for a coin flipping program that I'm making. the problem is the it seems to be exiting early. Take a look.
$(function() {
$('#rollDice').click(function() {
var e = document.getElementById("diceSides");
var diceSides = e.options[e.selectedIndex].text;
var diceRolls = document.getElementById('rollCount').value;
if (diceRolls.match(/^[\d]*$/ )) {
if (diceRolls == "") {
alert ("Please fill out all forms then try again.");
} else {
$('#diceRollContainer').slideDown('slow');
for (i=0;i<diceRolls;i++) {
var randNum = Math.floor(Math.random()*diceSides)+1;
var rolls = ("You rolled a " + diceSides + " sided die " + diceRolls + " times, and got the numbers ");
rollMinOne = rolls - 1;
if (i == rollMinOne) {
var rolls = (rolls + randNum + ".");
}
var rolls = (rolls + randNum + ", ");
}
alert (rolls);
}
} else {
alert ("Make sure you only enter numbers and no spaces, then try again.");
}
});
});
The problem is that the program is alerting rolls before the for loop seems to be completed. Why is it doing this?
You rolled a 4 sided die 3 times, and got the numbers 1, 2, 3.3,