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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Hangman letter glitch <solved>

    Hi, I have been working on a hangman game and I have run into a glitch. I have it coded so that when the user guesses a letter correct, the letter is filled into the corresponding text spot. Like this:
    Code:
    
    if (randomword.indexOf(guess1) == 0)
    {
    document.form1.letter1.value = guess1
    letter1 = guess1
    }
    
    if (randomword.indexOf(guess1) == 1)
    {
    document.form1.letter2.value = guess1
    letter2 = guess1
    }
    
    if (randomword.indexOf(guess1) ==2)
    {
    document.form1.letter3.value = guess1
    letter3 = guess1
    }
    
    if (randomword.indexOf(guess1) == 3)
    {
    document.form1.letter4.value = guess1
    letter4 = guess1
    }
    
    if (randomword.indexOf(guess1) == 4)
    {
    document.form1.letter5.value = guess1
    letter5 = guess1
    }
    
    if (randomword.indexOf(guess1) == 5)
    {
    document.form1.letter6.value = guess1
    letter6 = guess1
    }
    
    if (randomword.indexOf(guess1) == 6)
    {
    document.form1.letter7.value = guess1
    letter7 = guess1
    }
    
    if (randomword.indexOf(guess1) == 7)
    {
    document.form1.letter8.value = guess1
    letter8 = guess1
    }
    
    if (randomword.indexOf(guess1) == 8)
    {
    document.form1.letter9.value == guess1
    letter9 = guess1
    }
    
    if (randomword.indexOf(guess1) == 9)
    {
    document.form1.letter10.value = guess1
    letter10 = guess1
    }
    
    if (randomword.indexOf(guess1) == 10)
    {
    document.form1.letter11.value = guess1
    letter11 = guess1
    }
    The variable randomword is the randomly selected word from an array. The guess1 is the users guess. The letter variables are just to check to see if all the spots are filled to win the game. It works fine if the the word doesnt have repeating letters in it such as "Lucky". However words such as "Happy" do not work. If you guess 'p' code fills in the 3 text box with a 'P' but it leaves the 4 spot empty. Even if you guess 'p' again, it still leaves the 4 box empty. Thanks for the help! If you need to see the full code i can post it. Thanks again!
    Last edited by Mdev; 02-12-2010 at 02:09 AM. Reason: Solved

  • #2
    New Coder
    Join Date
    Dec 2009
    Posts
    82
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Adding a counter might work or a variation on it.

    Code:
    <script>
    var count = 0; //global var
    function checkWord() {
    if ((randomword.indexOf(guess1) == 0) && (count = 0))
    {
    document.form1.letter1.value = guess1;
    letter1 = guess1;
    count++;
    }
    
    if ((randomword.indexOf(guess1) == 1) && (count = 1))
    {
    document.form1.letter2.value = guess1
    letter2 = guess1
    count++;
    }

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hmmm I just tried that and it still doesnt work, thanks for trying though!

  • #4
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    You need to handle this task using a loop, addressing the single-letter fields thus:

    document.form1[ 'letter' + n ]

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Can you give an example of how to do the loop? Im pretty new to java so I don't really know what to do. I really apreciate the help!

  • #6
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by Mdev View Post
    Can you give an example of how to do the loop? Im pretty new to java so I don't really know what to do. I really apreciate the help!
    To have been given the hangman assignment, you must have covered loops.
    Code:
    for( var i = 0, len = randomword.length; i < len; i++ )
     if ( randomword.charAt( i ) == guess )
     {
      document.form1[ 'letter' + ( i + 1 ) ].value = guess;
      ...
     }

  • Users who have thanked randomuser773 for this post:

    Mdev (02-12-2010)

  • #7
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks so much it worked great! And im not currently taking a java class im just trying to learn java through books and the internet. Thanks again!

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Mdev View Post
    Thanks so much it worked great! And im not currently taking a java class im just trying to learn java through books and the internet. Thanks again!
    Be aware that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.


  •  

    Tags for this Thread

    Posting Permissions

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