-
Notifications
You must be signed in to change notification settings - Fork 0
/
app8.js
47 lines (40 loc) · 1013 Bytes
/
app8.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
const readline = require('readline')
function getRandomIntInclusive (min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1) + min)
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
let tries = 3
const randomNumber = getRandomIntInclusive(1, 10)
rl.setPrompt('Guess the number! (1 - 10): ')
rl.prompt()
function game (tries, randomNumber, guess) {
if (tries > 0) {
if (guess === randomNumber) {
console.log('WINNER')
process.exit(0)
} else if (guess > randomNumber) {
console.log('TOO HIGH')
} else if (guess < randomNumber) {
console.log('TOO LOW')
} else {
console.log('NOT A NUMBER')
}
} else {
if (guess === randomNumber) {
console.log('WINNER')
} else {
console.log(`YOU LOSE! THE NUMBER WAS: ${randomNumber}`)
}
process.exit(0)
}
}
rl.on('line', guess => {
tries--
game(tries, randomNumber, guess)
rl.prompt()
})