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!