View Full Version : Random Words from Array in Hangman

01-21-2009, 05:28 AM
Hi, I am learning Javascript from this book I got, and I am trying to do one of the "challenges" in the Hangman example, and that is to add on more 8 letter words to it and have it select by random. I am assuming that it's asking me to do this with arrays, but I have no idea how to implement that on this while having it select randomly. Can anyone please show me a way to do this?

<script language="JavaScript" type="text/javascript">
<form name="myform">
<input type="text" name="counter" readonly="true" size="10" />
<input type="text" name="letter1" readonly="true" size="3" />
<input type="text" name="letter2" readonly="true" size="3" />
<input type="text" name="letter3" readonly="true" size="3" />
<input type="text" name="letter4" readonly="true" size="3" />
<input type="text" name="letter5" readonly="true" size="3" />
<input type="text" name="letter6" readonly="true" size="3" />
<input type="text" name="letter7" readonly="true" size="3" />
<input type="text" name="letter8" readonly="true" size="3" />

<button onClick="select('a');">a</button>
<button onClick="select('b');">b</button>
<button onClick="select('c');">c</button>
<button onClick="select('d');">d</button>
<button onClick="select('e');">e</button>
<button onClick="select('f');">f</button>
<button onClick="select('g');">g</button>
<button onClick="select('h');">h</button>
<button onClick="select('i');">i</button>
<button onClick="select('j');">j</button>
<button onClick="select('k');">k</button>
<button onClick="select('l');">l</button>
<button onClick="select('m');">m</button>
<button onClick="select('n');">n</button>
<button onClick="select('o');">o</button>
<button onClick="select('p');">p</button>
<button onClick="select('q');">q</button>
<button onClick="select('r');">r</button>
<button onClick="select('s');">s</button>
<button onClick="select('t');">t</button>
<button onClick="select('u');">u</button>
<button onClick="select('v');">v</button>
<button onClick="select('w');">w</button>
<button onClick="select('x');">x</button>
<button onClick="select('y');">y</button>
<button onClick="select('z');">z</button>


<script language="JavaScript" type="text/javascript">

// 8 letter hangman - all words must have 8 letters

var word = "troubles";

var guesses = new Array(); // 0 means not guessed, 1 means guessed already

var timer = null;
var seconds;
function countDown ()
document.myform.counter.value = seconds;
seconds = seconds - 1;
if (seconds < 0)
alert ("Time expired");
clearInterval (timer);

function doTime() {

// get our word (already assigned to word)

// blank all of the text guesses
for (var i = 1; i <= 8; ++i)
document.forms.myform.elements["letter"+i].value = "";

// set up our guesses array
for (var j = 0; j < 8; ++ j)
guesses[j] = 0; // 0 means the letter hasn't been guessed yet

seconds = 30;
timer = setInterval("countDown()", 1000);

function select (letter) {
for (var i = 0; i < word.length; ++ i)
if (guesses[i] == 0)
if (word.charAt (i) == letter)
document.forms.myform.elements["letter" + (i+1)].value = letter;
guesses [i] = 1;
for (i = 0; i < word.length; ++ i)
if (guesses[i] == 0)
return; // not complete yet
alert ("You win");




01-21-2009, 06:26 AM
You will need a list of words you want to use set into an array like:

var WordArray ['aaaaaaaa','bbbbbbbb','cccccccc','dddddddd','eeeeeeee'];
function SelectRandomWord() {
var w = Math.random() * WordArray.length;
return WordArray[w];

Call the function when you want to retrieve a new word. Ii can also be use to pick other variable length words.

Philip M
01-21-2009, 09:11 AM

function SelectRandomWord() {
var w = Math.floor(Math.random() * WordArray.length);

"To a young heart everything is fun" - Charles Dickens

01-21-2009, 01:39 PM
PhillipM's right ... I type too fast for my own good. :(

Philip M
01-21-2009, 01:45 PM
Ensure that brain is engaged before operating keyboard. :D:D:D

01-21-2009, 07:29 PM
It was ... it's just that my transmission is worn out! :D:D:p:D:D