-
Notifications
You must be signed in to change notification settings - Fork 3
/
rockPaperScissors.js
54 lines (49 loc) · 1.28 KB
/
rockPaperScissors.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() {
let computerChoice = Math.random(Math.random() * 3);
if (computerChoice === 0) {
return "rock";
} else if (computerChoice === 1) {
return "scissors";
} else {
return "paper";
}
}
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "It's a tie!";
} else if (userChoice === "rock") {
if (computerChoice === "paper") {
return "Computer wins!";
} else {
return "You win!";
}
} else if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "Computer wins!";
} else {
return "You win!";
}
} else if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "Computer wins!";
} else {
return "You win!";
}
}
}
function playGame(input) {
let userChoice = input.toLowerCase();
let computerChoice = getComputerChoice();
console.log(`You threw ${userChoice}!`);
console.log(`Computer threw ${computerChoice}!`);
console.log(determineWinner(userChoice, computerChoice));
}
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(`Rock, paper or Scissors?`, (input) => {
playGame(input);
rl.close();
});