/
script.js
125 lines (111 loc) · 3.54 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
113
114
115
116
117
118
119
120
121
122
123
124
125
let rock = document.querySelector(".user-button.rock");
let paper = document.querySelector(".user-button.paper");
console.log(paper);
let scissors = document.querySelector('.user-button.scissors');
console.log(scissors);
let computerScore = 0;
let userScore = 0;
let rounds = 0;
function getComputerChoice() {
let choiceArr = ["Rock", "Paper", "Scissors"];
let choice = choiceArr[Math.floor(Math.random() * choiceArr.length)];
return (choice);
}
// Returns result of each round
function playRound (playerSelection, computerSelection) {
pSelect = playerSelection.toLowerCase();
cSelect = computerSelection.toLowerCase();
if (pSelect == cSelect) {
return ("It's a Tie!")
}
else if (pSelect == "rock")
{
if (cSelect == "paper")
{
computerScore++;
return ("You Lose! Paper beats Rock");
}
else if (cSelect == "scissors")
{
userScore++;
return ("You Win! Rock beats Scissors");
}
}
else if (pSelect == "scissors")
{
if (cSelect == "rock")
{
computerScore++;
return ("You Lose! Rock Beats Scissors");
}
else if (cSelect == "paper")
{
userScore++;
return("You Win! Scissors Beats Rock");
}
}
else if (pSelect == "paper")
{
if (cSelect == "rock")
{
userScore++;
return ("You Win! Paper Beats Rock");
}
else if (cSelect == "scissors")
{
computerScore++;
return ("You Lose! Scissors Beats Rock");
}
}
}
rock.addEventListener('click', function(e) {
let playerChoice = 'rock';
let ComputerChoice = getComputerChoice();
let result = playRound(playerChoice, ComputerChoice);
document.getElementById('comp-choice').textContent = ComputerChoice;
document.getElementById('user-choice').textContent = playerChoice;
document.getElementById('user-score').textContent = userScore;
document.getElementById('comp-score').textContent = computerScore;
if (userScore === 5){
alert("You Win!");
location.reload();
}
else if (computerScore === 5) {
alert("You Lose!");
location.reload();
}
})
paper.addEventListener('click', function(e) {
let playerChoice = 'paper';
let ComputerChoice = getComputerChoice();
let result = playRound(playerChoice, ComputerChoice);
document.getElementById('comp-choice').textContent = ComputerChoice;
document.getElementById('user-choice').textContent = playerChoice;
document.getElementById('user-score').textContent = userScore;
document.getElementById('comp-score').textContent = computerScore;
if (userScore === 5){
alert("You Win!");
location.reload();
}
else if (computerScore === 5) {
alert("You Lose!");
location.reload();
}
})
scissors.addEventListener('click', function(e) {
let playerChoice = 'scissors';
let ComputerChoice = getComputerChoice();
let result = playRound(playerChoice, ComputerChoice);
document.getElementById('comp-choice').textContent = ComputerChoice;
document.getElementById('user-choice').textContent = playerChoice;
document.getElementById('user-score').textContent = userScore;
document.getElementById('comp-score').textContent = computerScore;
if (userScore === 5){
alert("You Win!");
location.reload();
}
else if (computerScore === 5) {
alert("You Lose!");
location.reload();
}
})