-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
99 lines (84 loc) · 2.61 KB
/
javascript.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
function getComputerChoice() {
let computerChoice;
//get random int between and 3
let random = getRandomIntInclusive(1, 3);
//assign rock, paper, and scissors to int values
if (random === 1) {
computerChoice = "ROCK";
}
else if (random === 2){
computerChoice = "PAPER";
}
else {
computerChoice = "SCISSORS";
}
//return value
return computerChoice;
}
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
function playRound(playerSelection, computerSelection){
//tie conditions
if (playerSelection === computerSelection){
return 'tie';
}
//rock
if (playerSelection === "ROCK" && computerSelection === "SCISSORS"){
return 'win';
}
else if (playerSelection === "ROCK" && computerSelection === "PAPER"){
return 'loss';
}
//paper
if (playerSelection === "PAPER" && computerSelection === "ROCK"){
return 'win';
}
else if (playerSelection === "PAPER" && computerSelection === "SCISSORS"){
return 'loss';
}
//scissors
if (playerSelection === "SCISSORS" && computerSelection === "PAPER"){
return 'win';
}
else if (playerSelection === "SCISSORS" && computerSelection === "ROCK"){
return 'loss';
}
}
// result and score count variables
let res;
let winCount = 0;
let lossCount = 0;
let tieCount = 0;
// format results div and allow for score manipulation
const results = document.querySelector('.results');
const score = document.querySelector('.score');
score.textContent = "Wins: 0 Losses: 0 Ties: 0";
// register button clicks and increment counts for each round played
const button = document.querySelectorAll("button");
button.forEach((button) => {
button.addEventListener('click', function() {
// simulate each round based off of button click and random computer choice
let computer = getComputerChoice();
res = playRound(button.classList[0], computer);
// increment based off of results
if (res === 'win'){
winCount++;
} else if (res === 'loss'){
lossCount++;
} else if (res === 'tie'){
tieCount++;
}
// display scores
score.textContent = 'Wins: ' + winCount + ' Losses: ' + lossCount + ' Ties: ' + tieCount;
// display end result after 5 wins/losses
if (winCount >= 5){
score.textContent = 'You win!';
}
if (lossCount >= 5){
score.textContent = 'You lose.';
}
});
});