Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:40:23+00:00 2026-05-31T21:40:23+00:00

I’m having a problem with the following code snippet: <!DOCTYPE html> <html> <head> <title>Hangman</title>

  • 0

I’m having a problem with the following code snippet:

<!DOCTYPE html>
<html>
    <head>
        <title>Hangman</title>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

        <script type="text/javascript">
        <!--
        gallows = new Array("--------\n|      |\n|\n|\n|\n|\n=====",
        "--------\n|      O\n|\n|\n|\n|\n=====",
        "--------\n|      O\n|      |\n|\n|\n|\n=====",
        "--------\n|      O\n|     \\|\n|\n|\n|\n=====",
        "--------\n|      O\n|     \\|/\n|\n|\n|\n=====",
        "--------\n|      O\n|     \\|/\n|      |\n|\n|\n=====",
        "--------\n|      O\n|     \\|/\n|      |\n|     /\n|\n=====",
        "--------\n|      O\n|     \\|/\n|      |\n|     / \\\n|\n=====");

        guessChoices = new Array("JavaScript", "Navigator", "LiveConnect", "LiveWire");

        guessed = [];

        function startAgain()
        {
            guesses = 0;
            max = gallows.length - 1;
            //guessed = " ";
            len = guessChoices.length - 1;
            toGuess = guessChoices[Math.round(len*Math.random())].toUpperCase();
            displayHangman();
            displayToGuess();
            displayGuessed();
        }

        function stayAway()
        {
            document.game.elements[3].focus();
            alert("Don't mess with this form element!");
        }

        function displayHangman()
        {
            document.game.status.value=gallows[guesses];
        }

        function displayToGuess()
        {
            pattern = "";

            for(i=0;i<toGuess.length;++i)
            {
                if(guessed.indexOf(toGuess.charAt(i)) != -1)
                    pattern += (toGuess.charAt(i)+" ");
                else pattern += "_ ";
            }

            document.game.toGuess.value=pattern;
        }

        function displayGuessed(s)
        {

            result="";

            for(i in s)
            {
                guess=s[i];
                result += guess;
            }

            document.game.guessed.value=result;

            //document.game.guessed.value=guessed;
        }

        function badGuess(s)
        {
            if(toGuess.indexOf(s) == -1) return true;
            return false;
        }

        function winner()
        {
            for(i=0;i<toGuess.length;++i)
            {
                if(guessed.indexOf(toGuess.charAt(i)) == -1) return false;
            }
            return true;
        }

        function guess(s)
        {
            if(guessed.indexOf(s) == -1) guessed.push(s);
            if(badGuess(s)) ++guesses;
            displayHangman();
            displayToGuess();
            displayGuessed(guessed);
            if(guesses >= max)
            {
                alert("You're dead. The word you missed was "+toGuess+".");
                startAgain();
            }
            if(winner()) 
            {
                alert("You won!");
                startAgain();
            }
        }
        // -->
        </script>
    </head>
        <body>
            <h1>Hangman</h1>
            <form name="game">
                <pre>
                    <textarea name="status" rows="7" cols="16" onfocus="stayAway();"></textarea>
                </pre>

                <p>
                    <input type="text" name="toGuess" onfocus="stayAway();"> Word to guess<br>
                    <input type="text" name="guessed" onfocus="stayAway();"> Letters guessed so far<br>
                </p>

                <p>Enter your next guess.</p>

                <p>
                    <input type="text" name="input" size=1 value="">
                    <input type = "button" value = "guess" onclick = "guess(game.input.value); game.input.value = '';">
                </p>

                <input type="button" name="restart" value="---- Start Again ----" onclick="startAgain();">

                <script type="text/javascript">
                    <!--
                    startAgain();
                    // -->
                </script>
            </form>
        </body>
</html>

After I click the game.guess button, the first time around, I get the expected output… game.input clears and guessed is displayed. However, the second time I click the guess button with a different value, I receive the error:

guess is not a function
|   onclick()
|   event = click clientX=77, clientY=33

guess(game.input.value)                 onclick(line 2)

And I honestly can’t figure out why. What am I doing wrong?

(I got the bulk of this code from Mastering JavaScript, Premium Edition, by James Jaworski)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:40:25+00:00Added an answer on May 31, 2026 at 9:40 pm

    The problem is on these lines:

    guess=s[i];
    result += guess;
    

    This way you are changing the context of guess. If you change this line to

    result += s[i];
    

    Your error will go away.
    You are conflicting your function with using same name for a variable.
    Remember to define your variables using var keyword and avoid such confusion.

    1 – use anonymous functions where possible

    2 – declare and use local variables

    To avoid conflicts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.