-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (66 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const readlineSync = require('readline-sync');
const words = [
'javascript',
'code',
'talentpath',
'puppy',
'react',
'computer',
];
let again = false;
do {
const index = Math.floor(Math.random() * words.length);
const secret = words[index];
let revealSecret = '';
let strikes = 0;
let win = false;
let output = '';
for (let i = 0; i < secret.length; i++) {
revealSecret += '_';
}
revealSecret = revealSecret.trim();
output = '';
for (let i = 0; i < revealSecret.length; i++) {
output += `${revealSecret[i]} `;
}
output = output.trim();
console.log(output);
while (strikes < 5 && !win) {
const input = readlineSync.question('Pick a letter? ')[0];
if (input !== undefined) {
let reveal = '';
let correct = false;
win = true;
for (let i = 0; i < secret.length; i++) {
if (input === secret[i]) {
reveal += input;
correct = true;
} else if (revealSecret[i] !== '_') {
reveal += revealSecret[i];
} else {
reveal += '_';
win = false;
}
}
revealSecret = reveal;
if (correct) {
console.log('good guess');
} else {
strikes++;
console.log(`wrong ${strikes} strike(s)!`);
}
}
output = '';
for (let i = 0; i < revealSecret.length; i++) {
output += `${revealSecret[i]} `;
}
output = output.trim();
console.log(output);
}
if (win) {
console.log('YOU WON');
} else {
console.log('YOU LOSE');
}
again = readlineSync.keyInYNStrict('Do you want to play again?');
} while (again);