Skip to content

Commit e8f401f

Browse files
feat: solved 5th stage of the Hyperskill project "Hangman"
1 parent 87bbdb3 commit e8f401f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

hyperskill/09_hangman/05/.gitkeep

Whitespace-only changes.

hyperskill/09_hangman/05/main.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const input = require('sync-input');
2+
3+
const attempts = 8;
4+
const title = `H A N G M A N`;
5+
console.log(title);
6+
console.log();
7+
8+
const words = ["python", "java", "swift", "javascript"];
9+
const word = words[Math.floor(Math.random() * words.length)];
10+
11+
const hint = word.substring(0, 3) + "-".repeat(word.length - 3);
12+
13+
let revealedWord = "-".repeat(word.length);
14+
15+
for (let i = 0; i < attempts; i++) {
16+
console.log(revealedWord);
17+
const letter = input("Input a letter: ");
18+
19+
if (word.includes(letter)) {
20+
for (let j = 0; j < word.length; j++) {
21+
if (word[j] === letter) {
22+
revealedWord = revealedWord.substring(0, j) + letter + revealedWord.substring(j + 1);
23+
}
24+
}
25+
} else {
26+
console.log("That letter doesn't appear in the word");
27+
}
28+
}
29+
30+
console.log("Thanks for playing!");

0 commit comments

Comments
 (0)