-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessing-game.js
73 lines (60 loc) · 1.44 KB
/
guessing-game.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
const readline = require('node:readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let secretNumber;
let numAttempts;
const askGuess = () => {
numAttempts--;
rl.question("Enter a guess? ", checkGuess)
}
const checkGuess = number => {
if(Number(number) > secretNumber){
console.log("'Too high'")
if(numAttempts === 0){
console.log("'You lose!'")
rl.close()
}
else{
askGuess()
}
return false
}
else if(Number(number) < secretNumber){
console.log("'Too low'")
if(numAttempts === 0){
console.log("'You lose!'")
rl.close()
}
else{
askGuess()
}
return false
}
else{
console.log("'Correct!'")
console.log("'You Win!'")
rl.close()
}
}
const randomInRange = (min, max) => {
secretNumber = Math.floor(Math.random() * (max - min) + min)
}
const askRange = () => {
rl.question("Enter a max number: ", max)
}
const max = max => {
rl.question("Enter a min number: ", min => {
console.log(`I'm thinking of a number between ${min} and ${max}...`)
randomInRange(Number(min), Number(max))
askGuess()
})
}
const askLimit = () => {
rl.question("Enter a number of attempts: ", limit => {
numAttempts = limit
askRange()
})
}
askLimit()