The following functions operate within the PlayGame ( ) function and do all of the detailed work.

**ComputersTurn ()**

The computer is given the first turn. This may prove costly to the human player if the choice is for the computer to choose a random number between 1 and 30 because it could choose 1 or 2. Which would mean the computer would win on the first turn.

The computer uses integer division to make the choice of its move. It divides the Total by 3 and checks if the answer is equal to 2. If it is the computer deducts 2 from the total otherwise it deducts 1.

// Module Reference 2.1 function ComputersTurn () { // begin ComputersTurn // Computers's turn // Integer division to check for best move if ((Total % 3) == 2) { // begin if Total = Total - 2 ; alert ('Computer subtracted 2 from total. Total is now ' + Total) ; } // end if else { // begin else Total = Total - 1 ; alert ('Computer subtracted 1 from total. Total is now ' + Total) ; } // end else } // end ComputersTurn

Once the computer has had its turn the CheckForWin (PlayerID) function is called.

**CheckForWin (PlayerID)**

A string parameter of ‘Computer’ or ‘Human’ is passed to this function. This allows the function to be flexible enough to check for a computer win or a human win. If parameter passing were not used here it is likely that the program would include the CheckForWin code twice with a lot of duplicated code.

// Module Reference 2.2 function CheckForWin (PlayerID) { // begin CheckForWin // Declare and initialise local variables for use with ValidateData function var Minimum = 0 ; var Maximum = 0 ; // Check for winning result if (Total == 0) { // begin if alert ('The ' + PlayerID + ' wins!') ; GameOver = true ; } // end if } // end CheckForWin

If the computer does not make a winning move during its turn then the PlayersTurn () function will be called.

**PlayersTurn ()**

The PlayersTurn () function operates differently from the ComputersTurn () function mainly because there is no calculation involved only the opportunity for data input from the player. This data input, however, must only be a 1 or a 2. On order to make sure that this is the case the ValidateData (Minimum, Maximum) function is called.

// Module Reference 2.3 function PlayersTurn () { // begin PlayersTurn Number = prompt ('The total is : ' + Total + '\n' + 'How many do you want to subtract?') ; // Set range for validation Minimum = 1 ; Maximum = 2 ; ValidateData (Minimum, Maximum) ; Total = Total - Number ; // Take players valid data input away from Total } // end PlayersTurn

**ValidateData (Minimum, Maximum)**

The ValidateData (Minimum, Maximum) function takes two parameters. These values are set as a Minimum of 1 and a Maximum of 2. However, this function is flexible and will work with other values of minimum and maximum passed to it. This function will loop until the user enters a 1 or a 2.

// Module Reference 2.3.1 function ValidateData (Minimum, Maximum) { // begin ValidateData // Validate data entry to allow only a 1 or 2 to be entered while (Number < Minimum || Number > Maximum) { // begin while Number = prompt ('You can only enter ' + Minimum + ' OR ' + Maximum + '. Try again') ; } // end while } // end ValidateData