πŸͺ’ Build a Hangman

πŸ“Œ Mini project✍️ Written by Mark SullivanπŸ“… Reviewed 2026-04-25⏱ ~45 min read

CLI hangman with word list and guess tracking. Practices: strings, arrays, loops, conditionals.

What You'll Build

The classic hangman: a hidden word, the player guesses one letter at a time, you reveal correct letters and count wrong guesses up to 6.

Step 1 β€” the Setup

javascript
const words = ["javascript", "python", "concept", "function"];
const secret = words[Math.floor(Math.random() * words.length)];
const guessed = new Set();
let wrong = 0;
const MAX_WRONG = 6;

Step 2 β€” Render the Masked Word

javascript
function display() {
  return secret.split("").map(c => guessed.has(c) ? c : "_").join(" ");
}

Step 3 β€” the Loop

javascript
function guess(letter) {
  if (guessed.has(letter)) return "Already guessed";
  guessed.add(letter);
  if (!secret.includes(letter)) wrong++;
  if (display().split(" ").join("") === secret) return "WIN";
  if (wrong >= MAX_WRONG) return "LOSE";
  return "Continue";
}

Stretch Goals

  • Read words from a file.
  • Add categories (animals, programming, food).
  • Render the hangman ASCII art with each wrong guess.

Concepts You Just Used

M
Mark Sullivan
Lead writer Β· 8 yrs full-stack

Mark started coding in 2017 after switching from financial analysis. She's built production systems in Python (Django) and JavaScript (Node + React) at two startups, and has taught intro programming at his local community college since 2022. He owns the curriculum for variables, functions, conditionals, and loops on this site. More about Mark β†’