Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Words from Array in Hangman

    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?

    Code:
    <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>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Try this ...

    You will need a list of words you want to use set into an array like:
    Code:
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Correction:-

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


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

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts
    PhillipM's right ... I type too fast for my own good.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Ensure that brain is engaged before operating keyboard.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Smile

    It was ... it's just that my transmission is worn out!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •