-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
54 lines (48 loc) · 1.42 KB
/
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
function getComputerChoice() {
const num = Math.floor(Math.random() * 99) + 1;
if (num <= 33)
return 'r';
else if (num >= 66)
return 's';
else
return 'p';
}
function playRound() {
const comp = getComputerChoice();
let user_input;
while (true) {
user_input = prompt("Enter (r)rock, (p)papers, or (s)scissors:");
if (user_input === 'r' || user_input === 'p' || user_input === 's')
break;
else
alert("Invalid input! Please try again");
}
alert("Thinking...");
// Simulating thinking time
for (let i = 0; i < 1000000000; i++);
if (user_input === comp)
alert("Match draw");
else if ((user_input === 'r' && comp === 's') || (user_input === 'p' && comp === 'r') || (user_input === 's' && comp === 'p'))
alert("Congratulations! You won");
else
alert("You lost! Better luck next time");
}
function main() {
let loop_condition = true;
while (loop_condition) {
playRound();
let end_input;
while (true) {
end_input = prompt("Wanna play again? (y/n)").toLowerCase();
if (end_input === 'n') {
loop_condition = false;
break;
} else if (end_input === 'y') {
break;
} else {
alert("Invalid input! Please try again");
}
}
}
}
main();