If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Having problems with finishing up a hangman game.

So I have been working really hard trying to complete my hangman game, but I cannot find whats wrong with my code. As you can see, when you hit "start", one star pops up. I am trying to get the amount of letters in the hidden word pop up. Also, when I try and guess the letter of the word, nothing seems to happen. I am not looking for the answer, rather I am just looking for someone to point me in the correct direction to finish this. Here is my code. Any help is greatly appreciated.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<body>
<script>
var start = true;
var words = new Array("ANIMAL", "BREWERS", "FIERCE" , "XIOLPHONE" ,"BEAR", "BASEBALL", "PACKERS", "START", "ZEBRA");
var guess = "";
var show = ""; //display
var gone = ""; //used letters
var guesses= new Array ();

The problem seems to be in your updatedisplayword() function. When you initiate the loop you set the condition for i to be less than the variable 'length'. Honestly I'm at a loss as to exactly why, but you should simply replace this with 'guess.length'. At first I just thought it was an issue with variable types in the comparison but typeof returns number for both i and length. In any case that will correct your initial issue.

It does seem that going further there is another issue that will occur if you try to continue. You are using 'guess' as a string variable but also have a function called 'guess()'. This will create a 'string is not a function' error when you attempt to guess a letter using that function. Try changing the function name to 'guessLetter()' instead.

[EDIT]
Oh and I almost forgot to mention, please use code tags when pasting code in to your post.
@Logi Ali, interestingly enough, aside from the missing 'reset()' function being declared on the body's onload event, there weren't any errors in the console initially (other than his double-declared function and string which would only solve the later issue and only show up once a letter had been pressed).

Last edited by Sup3rkirby; 11-15-2012 at 03:25 PM.

"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

@Logi Ali, interestingly enough, aside from the missing 'reset()' function being declared on the body's onload event, there weren't any errors in the console initially (other than his double-declared function and string which would only solve the later issue and only show up once a letter had been pressed).

Nevertheless you'd still have to read them and fix them before addressing the algorithmic issues.

Where used, return should be executed unconditionally and always as the last statement in the function.

I'm not sure what issues you would still be having. The two corrections I mentioned fixed any issues with your script. Aside from a function that determines whether a user has won the game (or lost) everything was fine.

I suppose my question would be, did you manage to fix those two issues or are you still in the same place? If you did fix them, what issue(s) are you having now?

"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

I'm not sure what issues you would still be having. The two corrections I mentioned fixed any issues with your script. Aside from a function that determines whether a user has won the game (or lost) everything was fine.

I suppose my question would be, did you manage to fix those two issues or are you still in the same place? If you did fix them, what issue(s) are you having now?

I tried to fix the first issue, but it does not appear to have worked in the code. I guess part of it is that I do not have enough experience in the JS Field. Your help is greatly appreciated.

The updatedisplayword() correction just sets the loop to use guess.length for its condition directly, which fixes the issue with not displaying enough asterisks. The second issue was renaming your guess() function to guessLetter(). Alternatively, you could have just changed your guess variable to something else but in the end, you can't have a variable and function share the same name.

You should be able to finish it from this point on as you only need to update your guessed letters field and then determine whether or not a user has won or lost the game. That will at least finish your hangman game on a basic level.

"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

The updatedisplayword() correction just sets the loop to use guess.length for its condition directly, which fixes the issue with not displaying enough asterisks. The second issue was renaming your guess() function to guessLetter(). Alternatively, you could have just changed your guess variable to something else but in the end, you can't have a variable and function share the same name.

You should be able to finish it from this point on as you only need to update your guessed letters field and then determine whether or not a user has won or lost the game. That will at least finish your hangman game on a basic level.