-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (59 loc) · 2.37 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
72
// getComputerChoice() randomly returns "Rock", "Paper", or "Scissors"
function getComputerChoice() {
let gameChoices = ["Rock", "Paper", "Scissors"];
let randomIndex = Math.floor(Math.random() * gameChoices.length);
return (gameChoices[randomIndex]);
}
// storing the choice selected by the computer
const computerSelection = getComputerChoice();
// playerSelection
let playerSelection;
// playRound handles a round of gameplay and returns its result
function playRound (playerSelection, computerSelection) {
computerSelection = computerSelection.toLowerCase();
playerSelection = playerSelection.toLowerCase();
// tie!
if (playerSelection === computerSelection) {
return "It is a tie!";
}
// rock beats scissors
if (playerSelection === "rock" && computerSelection === "scissors") {
return "You win! Rock beats Scissors.";
} else if (playerSelection === "scissors" && computerSelection === "rock") {
return "You lose! Rock beats Scissors.";
// scissors beats paper
} else if (playerSelection === "scissors" && computerSelection === "paper") {
return "You win! Scissors beats Paper.";
} else if (playerSelection === "paper" && computerSelection === "scissors"){
return "You lose! Scissors beats Paper.";
// paper beats rock
} else if (playerSelection === "paper" && computerSelection === "rock") {
return "You win! Paper beats Rock.";
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "You lose! Paper beats Rock.";
// if input was note a game choice
} else {
return "Your input was not a game choice!"
}
}
// game() enables five rounds of the game
function game () {
// loop through 0-4 to play the game 5x
for (i = 0; i < 5; i++) {
if (i === 4){
do {
playerSelection = prompt("This is the final round, the ultimate battle. What totem do you choose?");
} while (!playerSelection);
let result = playRound(playerSelection, computerSelection);
console.log(result);
} else {
do{
playerSelection = prompt(`Choose a totem.`);
} while (!playerSelection);
let result = playRound(playerSelection, computerSelection);
console.log(result);
}
}
}
// calling game!
game();