Skip to content

Commit 8733b32

Browse files
feat: solved 6th stage of the Hyperskill project "Hangman"
1 parent e8f401f commit 8733b32

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

hyperskill/09_hangman/06/.gitkeep

Whitespace-only changes.

hyperskill/09_hangman/06/main.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const input = require('sync-input');
2+
3+
const title = `H A N G M A N`;
4+
console.log(title);
5+
console.log();
6+
7+
const words = ["python", "java", "swift", "javascript"];
8+
const word = words[Math.floor(Math.random() * words.length)];
9+
10+
const hint = word.substring(0, 3) + "-".repeat(word.length - 3);
11+
12+
let revealedWord = "-".repeat(word.length);
13+
let alreadyGuessed = new Set();
14+
15+
let lives = 8;
16+
while (lives > 0 && revealedWord !== word) {
17+
console.log(revealedWord);
18+
const letter = input("Input a letter: ");
19+
20+
if (alreadyGuessed.has(letter)) {
21+
console.log("No improvements");
22+
lives--;
23+
} else if (word.includes(letter)) {
24+
for (let j = 0; j < word.length; j++) {
25+
if (word[j] === letter) {
26+
revealedWord = revealedWord.substring(0, j) + letter + revealedWord.substring(j + 1);
27+
}
28+
}
29+
alreadyGuessed.add(letter);
30+
} else {
31+
console.log("That letter doesn't appear in the word");
32+
lives--;
33+
}
34+
}
35+
36+
if (revealedWord === word) {
37+
console.log("You guessed the word!");
38+
console.log("You survived!");
39+
} else {
40+
console.log("You lost!");
41+
}

0 commit comments

Comments
 (0)