Week 8: Hangman

To Do This Week


Hangman Game (5%)

Design a Hangman game using JavaScript, HTML, and CSS. Start with the in-class Logic Steps and then use JS Mentor to work out the syntax for JavaScript. The goal of this project is to understand the logic steps required for the game, to get experience translating logic steps into syntax, and then to style your game with CSS and to showcase how JavaScript interacts with HTML elements. Add a visual hangman figure using HTML, and control visibility with JavaScript. Get creative with your style and theme!

You may use JS Mentor Metaprompt for JS scripting assistance. Just upload to a new chat. You may generate the HTML and CSS outside of the chat, on your own or in another AI chat

Hangman Project Steps

Download this file for the exercise.


In-Class Work: The Hangman Logic Steps

What are the main action and processes of Hangman?

Write the Logic in Natural Language (Numbered Steps) on a sheet of paper or notebook

Write the program logic as a numbered list, ideally grouped around main functions. Each line should be one clear action or decision. Avoid code words at first. Just describe what happens.

Function Areas

     A) startGame()  // setup a fresh round, same as a reset

     B) guessLetter()  // handle the user's action (click or Enter)

     C) updateGameState(letter)  // modify the core game data based on the guess

     D) updateDisplay()  // reflect current state in the UI

     E) checkWinLose()  // determine end conditions

  

Mark the Steps That Need Variables, Conditionals, Arrays, Loops, and Functions

Next to your numbered steps, label with simple tags:

NOTE: Your Hangman game will likely need to use the following JavaScript actions:

As you label your steps, think carefully: Is this storing data? Repeating something? Making a decision? Updating the screen? That thinking process is more important than the final syntax.

Translate to “Code-Like” Pseudocode

Now type your steps as a comment at the top of your script area, so they sound closer to code, but still readable. Use short lines that map cleanly into JavaScript later.

Function Areas & Logic Steps

SET-UP (Before the game starts)
1. Create an array of possible words (wordsArray).
2. Store DOM elements in variables (so you can update the page):
   - wordDisplay (where the underscores/letters show)
   - message (where feedback shows)
   - lettersTriedDisplay (where tried letters show)
   - remainingDisplay (where guesses left show)
   - input (text box for the letter)
   - guessBtn (Guess button)
   - resetBtn (New Word button)
3. Create game-state variables (values that will change during play):
   - secretWord (string)
   - secretLetters (array)
   - shownWord (array)
   - triedLetters (array)
   - guessesLeft (number)
4. Connect event listeners:
   - guessBtn click -> guessLetter()
   - resetBtn click -> startGame()
5. Call startGame() to begin the first round.

A) startGame()  // set up a fresh round (also runs when New Word is clicked)
1. Clear the input and message, and turn the input and Guess button back on.
2. Pick a random secret word from the list.
3. Split the word into a letters array.
4. Create a shownWord array of underscores (same length).
5. Reset letters tried and guesses left.
6. Call updateDisplay().
7. Focus the input box.

B) guessLetter()  // runs when Guess is clicked
1. Read the letter from the input (make it lowercase).
2. If it was already tried, show a message and stop.
3. Add the letter to triedLetters.
4. Call updateGameState(letter).
5. Call updateDisplay().
6. Call checkWinLose().
7. Clear the input and focus it for the next guess.

C) updateGameState(letter)  // change the game data
1. Loop through the secretLetters array.
2. If the letter matches, reveal it in the shownWord array.
3. Track whether at least one match was found.
4. If no match, subtract one from guessesLeft.

D) updateDisplay()  // show the current state on the page
1. Show shownWord (with spaces between letters).
2. Show triedLetters (as a comma list).
3. Show remaining guesses.
4. Clear any old message.

E) checkWinLose()  // check for the end of the round
1. If shownWord.join("") equals secretWord, show a win message.
2. Else if guessesLeft equals 0, show a lose message with the word.
3. In either case, disable the input and Guess button.
  

Using the JS Mentor Metaprompt

JS Mentor Metaprompt is for JS scripting assistance. Just upload to a new chat. You may generate the HTML and CSS outside of the chat, on your own or in another AI chat Think of it as your personal JavaScript tutor. It helps you learn by guiding you step by step (without giving the answer to the full solution).

Start Your JS Mentor Chat

  1. Open a new chat in ChatGPT or Claude.
  2. Upload the JS Mentor — Assignment Mode document.
  3. Copy and paste the following message into the chat:

"Read the attached JS Mentor — Assignment Mode document and follow its tutoring instructions for this conversation.

Begin with the required Assignment Mode signature, then ask me about my JavaScript assignment. Do not provide assignment solutions.""

  1. Send the message along with the attached document.
  2. Confirm that the AI responds with JS MENTOR — ASSIGNMENT MODE ACTIVE.
  3. Share your assignment instructions and begin working through your logic steps and JavaScript code.

Important: Start each new assignment chat with the JS Mentor document and activation message. The AI is your learning coach, not your code generator.


Hangman