-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (58 loc) · 2.25 KB
/
script.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
// to play, run playGame() in the console.
// playGame();
const choices = [
"ROCK",
"PAPER",
"SCISSORS"
];
// randomizes an index of choices array
function computerPlay() {
return choices[Math.floor(Math.random() * choices.length)];
}
// initalizes scores
let playerScore = 0;
let computerScore = 0;
// plays one round of Rock, Paper, Scissors:
function playRound() {
// initializes round selections
const computerSelection = computerPlay();
const playerSelection = prompt("Pick one: Rock, Paper, or Scissors?").toUpperCase();
// logs round choices
console.log(`You chose ${playerSelection}`);
console.log(`Computer chose ${computerSelection}`);
// evaluates winner, increments points, and logs message
if (playerSelection == computerSelection) {
console.log("Whoa! It's a draw.");
} else if (playerSelection == "ROCK" && computerSelection == "SCISSORS" || playerSelection == "SCISSORS" && computerSelection == "PAPER" || playerSelection ==
"PAPER" && computerSelection == "ROCK" ) {
console.log(`You win! ${playerSelection} beats ${computerSelection}!`)
playerScore++;
} else {
console.log(`Boo, you lose! ${computerSelection} beats ${[playerSelection]}.`)
computerScore++;
}
// initializes scores array
let scores = [[`Player`, playerScore], [`Computer`, computerScore]];
// logs current Round score
console.log("----------------------");
console.log("Current Score:");
console.table(scores);
}
// plays a full game of Rock, Paper, Scissors
function playGame() {
// resets scores before each new game
playerScore = 0;
computerScore = 0;
// plays a full round 5x
for (let i = 0; i < 5; i++) {
playRound();
};
// evaluates game winner and logs message
if (playerScore > computerScore) {
console.log(`Wow, you won with ${playerScore} against the Computer's ${computerScore}! Play again?`);
} else if (playerScore == computerScore) {
console.log(`I can't believe it. You tied with the 'bot, ${playerScore} and ${computerScore}. Play again?`);
} else {
console.log(`You lost against a robot, ${computerScore} to ${playerScore}. Pitiful. Can your ego take another game?`);
}
}