I am trying to create a JavaScript version of an old timey dice game named PIG. It involves rolling dice and keeping track of the sum of each roll. I need to be able to save each roll within a function and then be able to call said function to return the current roll score.
This is the method that takes the amount of the current roll and stores it into the variable total, subsequently returning the current total variable.
function rollTotal(amount) {
return amount;
}
I first call the function and insert the score of the dice roll into the amount parameter...
var dieOne = 1;
var dieTwo = 2;
rollTotal(dieOne + dieTwo);
Then I call the function to return the current score like this...
rollTotal()
and am getting this...
undefined
Is there a way to set a default parameter to return the current score if a parameter is not entered when called?
amount = amount == null ? defaultValue : amountrollTotaldoesn't update any variable. It's just an identity function, sorollTotal()returnsundefinedinstead ofNaN.