-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
112 lines (94 loc) · 2.52 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const ROCK = "Rock";
const PAPER = "Paper";
const SCISSORS = "Scissors";
const EXIT = "Exit";
let playerScore = 0;
let computerScore = 0;
const results = document.querySelector("#results");
const score = document.querySelector("#score");
const buttons = document.querySelectorAll(".buttons button");
buttons.forEach((button) => {
button.addEventListener("click", buttonClick);
});
const resetButton = document.querySelector("#reset");
resetButton.addEventListener("click", reset);
function buttonClick() {
if (getWinState()) {
return;
}
const result = playRound(this.textContent, getComputerChoice());
results.textContent = result;
score.textContent = `Score: ${playerScore} (Player) to ${computerScore} (Computer)`;
let winState = getWinState();
if (winState) {
results.textContent = `Game Over! You ${winState}!`;
resetButton.classList.remove("invisible");
}
}
function reset() {
results.textContent = "Press a button to play!";
playerScore = computerScore = 0;
score.textContent = `Score: ${playerScore} (Player) to ${computerScore} (Computer)`;
resetButton.classList.add("invisible");
}
function getWinState() {
if (playerScore > 4) {
return "win";
} else if (computerScore > 4) {
return "lose";
} else {
return null;
}
}
function isValidInput(input) {
return input === ROCK || input === PAPER || input === SCISSORS;
}
function playRound(playerSelection, computerSelection) {
let result;
let playerWins;
if (playerSelection === computerSelection) {
return "It's a tie!";
}
switch (playerSelection) {
case ROCK:
playerWins = computerSelection === SCISSORS;
break;
case PAPER:
playerWins = computerSelection === ROCK;
break;
case SCISSORS:
playerWins = computerSelection === PAPER;
break;
default:
return "It's a tie!";
}
if (playerWins) {
result = `You win this round! ${playerSelection} beats ${computerSelection}`;
playerScore++;
} else {
result = `You lose this round! ${computerSelection} beats ${playerSelection}`;
computerScore++;
}
return result;
}
function getComputerChoice() {
const rand = Math.floor(Math.random() * 3);
let choice;
switch (rand) {
case 0:
choice = ROCK;
break;
case 1:
choice = PAPER;
break;
case 2:
default:
choice = SCISSORS;
break;
}
return choice;
}
function formatInput(input) {
const lowerCaseInput = input.toLowerCase().trim();
return lowerCaseInput.charAt(0).toUpperCase() + lowerCaseInput.slice(1);
}