-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
109 lines (99 loc) · 2.37 KB
/
scripts.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
//remove 5 round logic
//add third button repressing rock paper scissions
//convert all to DOM
//declaration
let rock = 2;
let paper = 0;
let scissor = 1;
let scoreUser = 0;
let scorePc = 0;
let paperButton = document.querySelector(".paper");
let scissorButton = document.querySelector(".scissor");
let rockButton = document.querySelector(".rock");
let displayScoreElement = document.querySelector(".score");
//event listeners
paperButton.addEventListener("click", paperFunction);
rockButton.addEventListener("click", rockFunction);
scissorButton.addEventListener("click", scissorFunction);
//functions
function getComputerChoice() {
//gets random number from 0 to 1 then converts it to 0,1 or 2
let randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return rock;
break;
case 1:
return paper;
break;
case 2:
return scissor;
break;
default:
return -1;
}
}
/**
*
* @param {String} am
* @returns {Number}
*/
function getUserChoice(am) {
switch (am) {
case "ROCK":
return rock;
break;
case "PAPER":
return paper;
break;
case "SCISSOR":
return scissor;
break;
}
}
/**
*
* @param {String} userChoiceDOM
* @returns {void}
*/
function determineWinner(userChoiceDOM) {
let pcChoice = getComputerChoice();
let userChoice = getUserChoice(userChoiceDOM);
//rock beats scissors
//scissors beats paper
//paper beats rock
if (
(userChoice == rock && pcChoice == scissor) ||
(userChoice == scissor && pcChoice == paper) ||
(userChoice == paper && pcChoice == rock)
) {
++scoreUser;
} else {
++scorePc;
}
displayScoreElement.textContent = `User's Score:${scoreUser} : Pc Score:${scorePc}`;
if (checkIfOver()) {
paperButton.removeEventListener("click", paperFunction);
rockButton.removeEventListener("click", rockFunction);
scissorButton.removeEventListener("click", scissorFunction);
}
}
function paperFunction() {
determineWinner("PAPER");
}
function rockFunction() {
determineWinner("ROCK");
}
function scissorFunction() {
determineWinner("SCISSOR");
}
function checkIfOver() {
if (scoreUser >= 5) {
displayScoreElement.textContent = `User Wins with ${scoreUser}`;
return true;
} else if (scorePc >= 5) {
displayScoreElement.textContent = `Pc Wins with ${scorePc}`;
return true;
}
return false;
}