Next page | Contents page |

Exercise H3 - Completing the game

You should now be able to complete the Hangman game.

More than 100 phrases are available in a separate script file for loading into your HTML along with your code. Save the target of this link so you do not see the contents of the file: phrases.js. It contains one variable called phrases declared as an array of Strings:

  var phrases = [ ..., ..., ... ];

We suggest each call of newGame() should select a phrase at random. There is a predefined function called Math.random(). It returns a random number in the range from 0.0 to 1.0. You need to scale that so the range is from 0.0 to (1 less than the length of the phrases array). Then, to use the result as index into the array you also need to ensure it is an integer. That can be done with another predefined function, Math.floor() - it chops off any fractional part of its parameter.

For the drawing steps we suggest creating an array of functions, each of which will draw one part of the drawing.

Next page | Contents page |