-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.js
40 lines (28 loc) · 766 Bytes
/
word.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
import { getRandomElement } from './util.js'
import { dictionary, maskChar } from './config.js'
class Word {
#word
#foundLetters
constructor() {
this.#word = getRandomElement(dictionary).toUpperCase()
this.#foundLetters = []
}
get word() {
return this.#word
}
get maskedWord(){
return [...this.#word]
.map(e => this.#foundLetters.includes(e) ? e : maskChar)
.reduce((p, c) => p + " " + c)
}
guess(letter) {
letter = letter.toUpperCase()
if (this.#word.indexOf(letter) > -1){
if (!this.#foundLetters.includes(letter)){
this.#foundLetters.push(letter)
}
return true
} return false
}
}
export default Word