I am working in Javascript and building a die roller function that would be called by various properties within an object. Alone, the rollDice function works properly, but within the object the random number generated by rollDice stays the same (unless I refresh the page). How do I get the rollDice function to update every time the property is called?
For instance, if I type rollDice(1, 6)
in the console I get a random number between 1 and 6. When I repeatedly type rollDice(1, 6)
, I get different numbers. Then, if I type availableWeapons.kick
, which should be the same as rollDice(1, 6)
, I get a random number between 1 and 6. When I repeatedly type availableWeapons.kick
, I do not get different numbers...I get the same number over and over again. When I refresh the page, that random number for availableWeapons.kick
will be different.
Thanks for your help!
// Dice
function rollDice(numDice, sides){
var result = 0;
for (let i = 0; i < numDice; i++) {
var diceRoll = Math.floor(Math.random() * sides) + 1;
result += diceRoll;
};
return result;
};
// Weapons
const availableWeapons = {
fist: rollDice(1, 3),
kick: rollDice(1, 6),
headButt: rollDice(1, 4),
epee: rollDice(1, 6) + 1,
revolver45: rollDice(1, 10) + 2,
gauge12: rollDice(4, 6)
};
Aucun commentaire:
Enregistrer un commentaire