Skip to content

Commit 8b71f5b

Browse files
feat: solved 8th stage of the Hyperskill project "Hangman"
1 parent 3676287 commit 8b71f5b

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

hyperskill/09_hangman/08/.gitkeep

Whitespace-only changes.

hyperskill/09_hangman/08/main.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const input = require('sync-input');
2+
3+
const readLetter = (revealedWord) => {
4+
let letter;
5+
while (true) {
6+
console.log(revealedWord)
7+
letter = input("Input a letter: ");
8+
if (letter.length !== 1) {
9+
console.log("Please, input a single letter.");
10+
} else if (!letter.match(/[a-z]/)) {
11+
console.log("Please, enter a lowercase letter from the English alphabet.");
12+
} else {
13+
break;
14+
}
15+
}
16+
17+
return letter;
18+
}
19+
20+
const showBanner = () => {
21+
console.log("H A N G M A N");
22+
console.log();
23+
}
24+
25+
const chooseWord = () => {
26+
const words = ["python", "java", "swift", "javascript"];
27+
return words[Math.floor(Math.random() * words.length)];
28+
}
29+
30+
const revealWord = (word, guessedLetters) => {
31+
let revealedWord = "";
32+
for (let i = 0; i < word.length; i++) {
33+
if (guessedLetters.has(word[i])) {
34+
revealedWord += word[i];
35+
} else {
36+
revealedWord += "-";
37+
}
38+
}
39+
40+
return revealedWord;
41+
}
42+
43+
let wins = 0;
44+
let losses = 0;
45+
46+
const showResult = (revealedWord, word) => {
47+
if (revealedWord === word) {
48+
console.log(`You guessed the word ${word}!`);
49+
console.log("You survived!");
50+
wins++;
51+
} else {
52+
console.log("You lost!");
53+
losses++;
54+
}
55+
}
56+
57+
const game = () => {
58+
const word = chooseWord();
59+
60+
let revealedWord = "-".repeat(word.length);
61+
let alreadyGuessed = new Set();
62+
63+
let lives = 8;
64+
while (lives > 0 && revealedWord !== word) {
65+
const letter = readLetter(revealedWord);
66+
67+
if (alreadyGuessed.has(letter)) {
68+
console.log("You've already guessed this letter.");
69+
} else if (word.includes(letter)) {
70+
alreadyGuessed.add(letter);
71+
revealedWord = revealWord(word, alreadyGuessed);
72+
} else {
73+
console.log("That letter doesn't appear in the word");
74+
alreadyGuessed.add(letter);
75+
lives--;
76+
}
77+
}
78+
79+
showResult(revealedWord, word);
80+
}
81+
82+
const showMenu = () => {
83+
return input(`Type "play" to play the game, "results" to show the scoreboard, and "exit" to quit:`);
84+
}
85+
86+
const hangman = () => {
87+
showBanner();
88+
89+
while (true) {
90+
switch (showMenu()) {
91+
case "play":
92+
game();
93+
break;
94+
case "results":
95+
console.log(`You won: ${wins} times.`);
96+
console.log(`You lost: ${losses} times.`);
97+
break;
98+
case "exit":
99+
return;
100+
default:
101+
console.log("Invalid command!");
102+
}
103+
}
104+
}
105+
106+
hangman();

hyperskill/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ There are no certificates for this track yet.
3232

3333
[My](https://hyperskill.org/profile/7889902) solutions to [Hyperskill](https://hyperskill.org) projects from the [JavaScript Core](https://hyperskill.org/tracks/32) track.
3434

35-
| Project | Level | Status |
36-
| --------------------------------------------- | ------ | ----------- |
37-
| [Chalkboard Printer](./08_chalkboard_printer) | Medium | Completed |
38-
| [Hangman (JavaScript)](./09_hangman) | Hard | In Progress |
35+
| Project | Level | Status |
36+
| --------------------------------------------- | ------ | --------- |
37+
| [Chalkboard Printer](./08_chalkboard_printer) | Medium | Completed |
38+
| [Hangman (JavaScript)](./09_hangman) | Hard | Completed |
39+
40+
My [certificate](https://hyperskill.org/certificates/e4756834-a763-4bf1-9cf4-3e1cb141ca51.pdf) of completion the track.
Binary file not shown.

0 commit comments

Comments
 (0)