-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock-paper-scissors.js
93 lines (70 loc) · 2.42 KB
/
rock-paper-scissors.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
const choices = ["Rock", "Paper", "Scissors"];
function getComputerChoice() {
let randomNumber = Math.floor(Math.random() * choices.length);
return choices[randomNumber]
}
const rockBtn = document.querySelector("#rock");
const paperBtn = document.querySelector("#paper");
const scissorsBtn = document.querySelector("#scissors");
const runningScore = document.querySelector("#runningScore")
const roundScore = document.querySelector("#Roundscore");
const human = document.querySelector("#human");
const computer = document.querySelector("#computer");
const finalScore = document.querySelector("#finalScore");
let humanScore = 0;
let computerScore = 0;
const targetScore = 5;
let isGameOver = false;
rockBtn.addEventListener("click", function() {
if (!isGameOver){
playRound("Rock", getComputerChoice());
endGame();
}
});
paperBtn.addEventListener("click", function() {
if (!isGameOver){
playRound("Paper", getComputerChoice());
endGame();
}
});
scissorsBtn.addEventListener("click", function() {
if (!isGameOver){
playRound("Scissors" , getComputerChoice());
endGame();
}
});
function playRound(humanChoice, computerChoice) {
const newLi = document.createElement("li");
newLi.classList.add("newLi");
if ((humanChoice === "Rock" && computerChoice === "Scissors"
|| (humanChoice === "Scissors" && computerChoice === "Paper"))
|| (humanChoice === "Paper" && computerChoice === "Rock"))
{
newLi.innerText = `You win, ${humanChoice} beats ${computerChoice}!`;
roundScore.appendChild(newLi);
humanScore++;
human.innerText = humanScore;
}
else if (humanChoice == computerChoice) {
newLi.innerText = `Draw, both picked ${humanChoice}`;
roundScore.appendChild(newLi);
} else {
newLi.innerText = `You lose, ${computerChoice} beats ${humanChoice}!`;
roundScore.appendChild(newLi);
computerScore++;
computer.innerText = computerScore;
}
}
function endGame() {
if (humanScore === targetScore || computerScore === targetScore){
isGameOver = true;
}
let gameOverString = ``;
if (humanScore === targetScore) {
gameOverString = `You won the game! Poggers!`;
}
else if (computerScore === targetScore){
gameOverString = `You lost the game, F in the chat.`;
}
finalScore.innerText = gameOverString;
}