-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess.js
More file actions
31 lines (31 loc) · 797 Bytes
/
guess.js
File metadata and controls
31 lines (31 loc) · 797 Bytes
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
let max = parseInt(prompt("enter your maximum no."));// parsing int will acept the no. only
while (!max) {
max = parseInt(prompt("please enter a valid no."))
}
const target = Math.floor(Math.random() * max) + 1;
let guess = prompt("let your first guess");
let count = 1;
while (target !== parseInt(guess)) {
if (guess === 'q') {
break;
}
guess = parseInt(guess);
if (target > guess) {
guess = prompt("too low. try again");
count++;
}
else if (guess > target) {
guess = prompt("too high. try again")
count++;
}
else {
guess = prompt("invalid guess");
}
}
if (guess === 'q') {
console.log("you quit, you loser")
}
else {
console.log("you won fucker");
console.log(`it took you ${count} guesses`);
}