-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (61 loc) · 1.91 KB
/
app.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
74
75
// Rock Paper Scissors game
// get random number
const randomNumberGenerator = () => {
return Math.floor(Math.random() * 3);
};
// get rock, paper, scissors randomly for computer
const computerPlay = () => {
let randomNumber = randomNumberGenerator();
let computerChoice;
switch (randomNumber) {
case 0:
computerChoice = "rock";
break;
case 1:
computerChoice = "paper";
break;
case 2:
computerChoice = "scissors";
break;
default:
console.log("Error");
break;
}
return computerChoice;
};
// static player selection -- commenting out since we are getting a user input below
// const playerChoice = "scissors";
// play round
const playRound = (playerChoice) => {
let computerChoice = computerPlay();
if (playerChoice === computerChoice) {
console.log(`Tie, both players selected ${playerChoice}`);
} else {
if (playerChoice === "rock" && computerChoice === "scissors") {
console.log(`Player wins! ${playerChoice} beats ${computerChoice}`);
} else if (playerChoice === "paper" && computerChoice === "rock") {
console.log(`Player wins! ${playerChoice} beats ${computerChoice}`);
} else if (playerChoice === "scissors" && computerChoice === "paper") {
console.log(`Player wins! ${playerChoice} beats ${computerChoice}`);
} else {
console.log(`Computer wins! ${computerChoice} beats ${playerChoice}`);
}
}
};
// prompt player for choice
const getPlayerChoice = () => {
let playerChoice = prompt("Please enter rock, paper, or scissors.");
playerChoice = playerChoice.toLowerCase();
console.log(`You chose ${playerChoice}`);
return playerChoice;
};
// play game 5 times with user input
const game = () => {
playRound(getPlayerChoice());
playRound(getPlayerChoice());
playRound(getPlayerChoice());
playRound(getPlayerChoice());
playRound(getPlayerChoice());
};
// execute play function!!!
game();