I am trying to make a Yahtzee game. I have a function to check if the dice rolled are a small straight.
var sortedDice = rollDice().sort(); // rollDice() generates an array with 5 random numbers
My function to determine if there is a small straight:
function isSmStraight(checkSmStraight){
var smStraight = false;
var i = 1;
var j = 0;
//will remove a die if there are duplicates
while(i < checkSmStraight.length){
if (checkSmStraight[i] == checkSmStraight[j]){
i++;
} else {
j++;
checkSmStraight[j] = checkSmStraight[i];
i++;
}//end if else
}//end while loop that moves duplicates to last index of array
checkSmStraight.pop();//removes last index of array
if (isLgStraight(checkSmStraight) == true){
smStraight = true;
} else if (checkSmStraight.length == 4 && checkSmStraight[checkSmStraight.length-1] - checkSmStraight[0] == 3){
smStraight = true;
}//end if else if
return smStraight;
}//end function isSmStraight()
I have copied sortedDice to another array, fourDice, that I can use to call isSmStraight(). I want just this function to use the four-index array, but it always messes with sortedDice, so the rest of the program uses the four-dice array. (This isn't the whole program, just the parts I thought are relevant. Also, the program is finished, I'm just trying to get the scoring functions right first).
var newvar = ""; function test() { newvar = "new text"; }.slice()to create a copy.