Forum Moderators: open

Message Too Old, No Replies

Random Words from Array in Hangman

         

majinbejitto

5:40 am on Jan 15, 2009 (gmt 0)

10+ Year Member



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?

<html>
<head>
<title>Hangman</title>
<script language="JavaScript" type="text/javascript">
</script>
</head>
<body>
<form name="myform">
<input type="text" name="counter" readonly="true" size="10" />
<br>
<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" />

<br>
<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>
<br>
<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>
<br>
<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>
<br>
<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>
<br>
<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>

</form>

<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
clearInterval(timer);
alert ("You win");
}

doTime();

</script>

</body>
</html>

jalarie

6:23 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



I have the following code on one of my pages:

Locations=new Array(
"Seatle",
"New York",
"London",
"Paris",
"Berlin",
"Vadodara, India"
);
function Pick(ListName) {
return ListName[Random(ListName.length,0)];
} // Pick
function Random(Range,Base) {
return Math.floor(Math.random()*(Range-Base)+Base);
} // Random

Location1=Pick(Locations);

Fotiman

7:26 pm on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! First, I would change a few things.
1. Remove the language attribute on your script tag. This is invalid.
2. You seem to have a mix of XHTML and HTML tags. Your input tags have /> instead of >.
3. You have an empty <script> in your <head>.

Now, as for you JavaScript:
1. I would recommend using [] instead of new Array(). For example:


var guesses = [];

This avoids the overhead of new and is shorter.

2. Instead of seconds = seconds - 1, use the decrement operator:


seconds--;

3. Always use curly brackets around your for and if/else blocks.


for (var i = 1; i <= 8; ++i) {
document.forms.myform.elements["letter"+i].value = "";
}


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

etc.

4. When using setInterval (or setTimeout), pass a function reference instead of a string:


timer = setInterval(countDown, 1000);

Now, lets say you have an array of words:


var words = [
"footlong",
"headrest",
"bookmark"
];

What you need to do is get a random number between 0 (the index of your first element in the array) and 2 (the length of the array - 1 because it's a zero based array). To do this, use Math.floor() and Math.random():


var randomNum = Math.floor(Math.random() * words.length);

Math.random returns a number between 0 (inclusive) and 1 (exclusive), so multiply that by your array length to get a number between 0 (0 * array length = 0) and some number just under your array length (.999 * 3 = 2.997), then use Math.floor to return the nearest rounded down integer.

Now, just use that index to get a word from your array.

Fotiman

7:32 pm on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, you could also create a generic function for getting random integers between a certain range:


/**
* @param {int} min The minimum number to return
* @param {int} max The maximum number to return
* @returns A random integer between min and max
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

So you could call getRandomInt(0, words.length - 1) to get your random number.