I am working on a game that has three dice cubes that show random faces upon clicking a button. Images are by css image sprite. If the random number gets 1 the dice cube is assigned to a css class that has its image sprite.
function diceroll (){
var randomnumber = Math.floor(Math.random() * (6 - 1 + 1)) + 1;
switch (randomnumber) {
case 1:
document.getElementById("dice1").setAttribute("class", "face1");
break;
case 2:
document.getElementById("dice1").setAttribute("class", "face2");
break;
case 3:
document.getElementById("dice1").setAttribute("class", "face3");
break;
case 4:
document.getElementById("dice1").setAttribute("class", "face4");
break;
case 5:
document.getElementById("dice1").setAttribute("class", "face5");
break;
case 6:
document.getElementById("dice1").setAttribute("class", "face6");
break;
}
}
I have a separate button, when clicked it should run the above diceroll function to the three divs with ids dice1, dice2 and dice3.
I want to use
function gotoloop (){
for (i = 0; i < 2; i++) {
// the code that affects dice(n) and n=1 and then diceroll function
// affects dice1 n+1
}
}
I researched and could not find a way to implement the code for the last commented two lines. Please let me know if my approach is correct and help me with the code.