Is it possible to make these three functions at the top into one and still not get the same random nr at each dice, when i click a button to roll all three? to clearify the functions are for when i click on a single dice, and the function at the bottom is for when i want to roll all three att the same time! all of it works my question is only if it could be done with less code?
function rollDice1(){
var randomDice = Math.floor(6*Math.random())+1;
dice1.src = "dice/" + randomDice + ".jpg";
}
function rollDice2(){
var randomDice = Math.floor(6*Math.random())+1;
dice2.src = "dice/" + randomDice + ".jpg";
}
function rollDice3(){
var randomDice = Math.floor(6*Math.random())+1;
dice3.src = "dice/" + randomDice + ".jpg";
}
function rollDices() {
rollDice1();
rollDice2();
rollDice3();
}
Is it possible to make these three functions at the top into one and still not get the same random nr at each dice, when i click a button to roll all three? to clearify the functions are for when i click on a single dice, and the function at the bottom is for when i want to roll all three att the same time! all of it works my question is only if it could be done with less code?
function rollDice1(){
var randomDice = Math.floor(6*Math.random())+1;
dice1.src = "dice/" + randomDice + ".jpg";
}
function rollDice2(){
var randomDice = Math.floor(6*Math.random())+1;
dice2.src = "dice/" + randomDice + ".jpg";
}
function rollDice3(){
var randomDice = Math.floor(6*Math.random())+1;
dice3.src = "dice/" + randomDice + ".jpg";
}
function rollDices() {
rollDice1();
rollDice2();
rollDice3();
}
dice1
, dice2
and dice3
?
– Denys Séguret
Commented
Mar 24, 2013 at 19:57
Add a parameter to the function so when called you can pass in the die to be set. This assumes dice1
, dice2
and dice3
are global variables.
function rollDice(di){
var randomDice = Math.floor(6*Math.random())+1;
di.src = "dice/" + randomDice + ".jpg";
}
function rollDices() {
rollDice(dice1);
rollDice(dice2);
rollDice(dice3);
}
You can loop, no need to call an external function thrice :
function rollDices() {
for (var i=1; i<=3; i++) {
var randomDice = Math.floor(6*Math.random())+1;
window['dice'+i].src = "dice/" + randomDice + ".jpg";
}
}
It could be better by having an array instead of three separate variables for dice1
, dice2
and dice3
(I supposed here that they were global variables).
I would actually prefer to go with an OO-approach; simply created different Dice-objects that can be rolled.
function Dice() {
var self = this;
this.face;
this.roll = function () {
var randomDice = Math.floor(6*Math.random())+1;
self.face = "dice/" + randomDice + ".jpg";
return randomDice;
};
};
var dice1 = new Dice(),
dice2 = new Dice(),
dice3 = new Dice();
dice1.roll();
dice2.roll();
dice3.roll();
console.log(dice1.face);
console.log(dice2.face);
console.log(dice3.face);
JSFiddle example.
(Take note it's inplete; when the Dice hasn't been rolled, the face is undefined
. You might want to take steps in order to prevent that state).
My approach is:
function rollDice(times){
var randomDices = [];
while (randomDices.length < times) {
var rand = Math.floor(6*Math.random())+1;
if(randomDices.indexOf(rand) > -1 == false)
randomDices.push(rand);
}
return randomDices
}
The above code returns an array, with length you specify, of random dices.
for example rollDice(3)
will return three random dices.
Here's my variation on the accepted answer :
function rollDice(di) {
var randomDice = Math.floor(6*Math.random())+1;
di.src = "dice/" + randomDice + ".jpg";
}
function rollDices(arr) {
for (i = 0; i < arr.length; i++) {
rollDice(arr[i]);
}
}
An advantage of this implementation, is that you can use any number of dice and pass along the image variables to rollDices
in an array.
To roll one dice, you could do this :
rollDice(dice1);
However, you could also do this :
rollDices([dice1]);
To roll two dices, you could do this :
rollDices([dice1, dice2]);
To roll three dices, you could do this :
rollDices([dice1, dice2, dice3]);
To roll four dices, you could do this :
rollDices([dice1, dice2, dice3, dice4]);
Etc...