-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (101 loc) · 3.2 KB
/
Copy pathscript.js
File metadata and controls
116 lines (101 loc) · 3.2 KB
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
113
114
115
116
// Query selectors
const cta = document.querySelector("#cta");
const result = document.querySelector("#result");
const yourScore = document.querySelector("#yourScore");
const compScore = document.querySelector("#compScore");
// creating buttons for game
const rock = document.createElement("button");
rock.textContent = "Play Rock";
cta.appendChild(rock);
const paper = document.createElement("button");
paper.textContent = "Play Paper";
cta.appendChild(paper);
const scissors = document.createElement("button");
scissors.textContent = "Play Scissors";
cta.appendChild(scissors);
function getComputerChoice() {
const randomNumber = Math.floor(Math.random() * 100);
if (randomNumber <= 33) {
return "rock";
} else if (randomNumber > 33 && randomNumber <= 66) {
return "paper";
} else {
return "scissors";
}
}
// Declare players' score
let humanScore = 0;
let computerScore = 0;
let computerChoice = getComputerChoice();
// Playing logic
function playRound(humanChoice, computerChoice) {
// let humanChoice = rawHumanChoice.toLowerCase();
// Human chooses rock
if (humanChoice === "rock") {
if (computerChoice === "rock") {
result.textContent = "It's a tie!";
} else if (computerChoice === "paper") {
computerScore++;
result.textContent = "You lose! Paper beats rock.";
} else if (computerChoice === "scissors") {
humanScore++;
result.textContent = "You win! Rock beats scissors.";
}
// Human chooses paper
} else if (humanChoice == "paper") {
if (computerChoice == "rock") {
humanScore++;
result.textContent = "You win! Paper beats rock.";
} else if (computerChoice == "paper") {
result.textContent = "It's a tie!";
} else {
computerScore++;
result.textContent = "You lose! Scissors beat paper.";
}
//Human chooses scissors
} else if (humanChoice == "scissors") {
if (computerChoice == "rock") {
computerScore++;
result.textContent = "You lose! Rock beats scissors.";
} else if (computerChoice == "paper") {
humanScore++;
result.textContent = "You win! Scissors beat paper.";
} else {
result.textContent = "It's a tie!";
}
}
function checkWinner() {
if (humanScore == 5 && computerScore < humanScore) {
yourScore.textContent = "You win!";
compScore.textContent = "Computer lost";
humanScore = 0;
computerScore = 0;
} else if (compScore == 5 && humanScore < computerScore) {
yourScore.textContent = "You lose";
compScore.textContent = "Computer wins!";
humanScore = 0;
computerScore = 0;
}
}
yourScore.textContent = humanScore + " / 5";
compScore.textContent = computerScore + " / 5";
checkWinner();
}
//Play rock button
function playRock() {
computerChoice = getComputerChoice();
playRound("rock", computerChoice);
}
rock.addEventListener("click", playRock);
//Play paper button
function playPaper() {
computerChoice = getComputerChoice();
playRound("paper", computerChoice);
}
paper.addEventListener("click", playPaper);
//Play scissors button
function playScissors() {
computerChoice = getComputerChoice();
playRound("scissors", computerChoice);
}
scissors.addEventListener("click", playScissors);