πͺ’ Build a Hangman
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.