-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
107 lines (91 loc) · 2.58 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
const picks = ["rock", "paper", "scissors"];
const rockButton = document.querySelector(".rock");
const paperButton = document.querySelector(".paper");
const scissorsButton = document.querySelector(".scissors");
const scoreLine = document.querySelector(".score");
const compPickText = document.querySelector(".comp-pick-text");
let compPickIndex = undefined;
let compPick = undefined;
let playerPick = undefined;
let scoreLineText = undefined;
let compScore = 0;
let playerScore = 0;
let tempResult = undefined;
function updateScore(){
scoreLineText = `Computer: ${compScore} - ${playerScore} : You`
scoreLine.textContent = scoreLineText;
}
function setCompPick(){
let compPickIndex = Math.floor((Math.random())*3);
compPick = picks[compPickIndex];
}
rockButton.addEventListener("click", ()=>{
playerPick = "rock";
round();
})
paperButton.addEventListener("click", ()=>{
playerPick = "paper";
round();
})
scissorsButton.addEventListener("click", ()=>{
playerPick = "scissors";
round();
})
function endGame(){
if (playerScore == 5){
compPickText.textContent = `You won 5-${compScore}`;
} else if (compScore == 5){
compPickText.textContent = `You lost 5-${playerScore}`;
}
playerScore = 0;
compScore = 0;
updateScore();
}
function round(){
setCompPick();
if (playerPick=="rock"){
switch(compPick){
case "rock":
tempResult = "It's a draw";
break;
case "paper":
tempResult = "You Lost";
compScore++
break;
case "scissors":
tempResult = "You won";
playerScore++
}
} else if (playerPick=="paper"){
switch(compPick){
case "paper":
tempResult = "It's a draw";
break;
case "scissors":
tempResult = "You Lost";
compScore++
break;
case "rock":
tempResult = "You won";
playerScore++
}
} else {
switch(compPick){
case "scissors":
tempResult = "It's a draw";
break;
case "rock":
tempResult = "You Lost";
compScore++
break;
case "paper":
tempResult = "You won";
playerScore++
}
}
compPickText.textContent = `Computer picked ${compPick}: ${tempResult}`;
updateScore();
if (playerScore == 5 || compScore==5){
endGame();
}
}