Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
48 lines (46 loc) · 1.41 KB
/
Copy pathindex.ts
File metadata and controls
48 lines (46 loc) · 1.41 KB
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
import { prompt } from 'inquirer';
import * as chalk from 'chalk';
const MAX_GUESSES = 6;
let word: string;
const getGuess = async (): Promise<string> => {
let guess = '';
while(guess.length !== 5){
guess = (await prompt([{type: 'input', name: 'guess', message: 'Guess a word'}])).guess.toLowerCase();
if (guess.length !== 5) {
console.log('Guess was not 5 characters, please try again')
}
}
return guess;
}
const processGuess = async (guess: string) => {
// letter correct place? green
// correct letter: yellow
// gray
let output: string = ""
for (let i = 0; i < guess.length; i++) {
const letter = guess[i];
if(word[i] === letter){
output += (chalk.green(letter));
} else if(word.includes(letter)){
output += (chalk.yellowBright(letter));
} else {
output += (chalk.grey(letter))
}
}
console.log(output)
}
prompt([{type: 'input', name: 'word', message: 'What is the word to be guessed?'}]).then(async (answer) => {
console.clear();
word = answer.word as string;
word = word.toLowerCase();
for (let missedGuessCount = 0; missedGuessCount < MAX_GUESSES; missedGuessCount++) {
const guess = await getGuess();
if (guess === word){
console.log("YOU GOT IT!");
return;
}
await processGuess(guess)
console.log({guess, remainingGuesses: MAX_GUESSES - 1 - missedGuessCount})
}
console.log(`Good try! The word was ${word}`)
})