Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2010, 09:42 PM   PM User | #1
Mdev
New to the CF scene

 
Join Date: Feb 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Mdev is an unknown quantity at this point
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
Mdev is offline   Reply With Quote
Old 02-11-2010, 10:24 PM   PM User | #2
harrierdh
New Coder

 
Join Date: Dec 2009
Posts: 82
Thanks: 0
Thanked 6 Times in 6 Posts
harrierdh is an unknown quantity at this point
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++;
}
harrierdh is offline   Reply With Quote
Old 02-11-2010, 10:57 PM   PM User | #3
Mdev
New to the CF scene

 
Join Date: Feb 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Mdev is an unknown quantity at this point
Hmmm I just tried that and it still doesnt work, thanks for trying though!
Mdev is offline   Reply With Quote
Old 02-11-2010, 11:18 PM   PM User | #4
randomuser773
Banned

 
Join Date: Nov 2008
Location: not found
Posts: 284
Thanks: 0
Thanked 53 Times in 51 Posts
randomuser773 can only hope to improve
You need to handle this task using a loop, addressing the single-letter fields thus:

document.form1[ 'letter' + n ]
randomuser773 is offline   Reply With Quote
Old 02-12-2010, 12:14 AM   PM User | #5
Mdev
New to the CF scene

 
Join Date: Feb 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Mdev is an unknown quantity at this point
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!
Mdev is offline   Reply With Quote
Old 02-12-2010, 01:09 AM   PM User | #6
randomuser773
Banned

 
Join Date: Nov 2008
Location: not found
Posts: 284
Thanks: 0
Thanked 53 Times in 51 Posts
randomuser773 can only hope to improve
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;
  ...
 }
randomuser773 is offline   Reply With Quote
Users who have thanked randomuser773 for this post:
Mdev (02-12-2010)
Old 02-12-2010, 02:07 AM   PM User | #7
Mdev
New to the CF scene

 
Join Date: Feb 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Mdev is an unknown quantity at this point
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!
Mdev is offline   Reply With Quote
Old 02-12-2010, 07:55 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
hangman, letters, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:42 PM.


Advertisement
Log in to turn off these ads.