-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (62 loc) · 2.07 KB
/
app.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
//randomly return Rock Paper or Scissors based on a random number generated
function getComputerChoice(){
let rndNum = Math.floor(Math.random()*3);
switch(rndNum){
case 0:
return "rock";
break;
case 1:
return "paper";
break;
case 2:
return "scissors";
break;
}
}
function rPSRound(computerChoice,playerChoice){
if(computerChoice === playerChoice){
return "Tie no winner";
}else if(playerChoice == "rock" &&
computerChoice == "paper" ||
playerChoice == "scissors" &&
computerChoice == "rock" ||
playerChoice == "paper" &&
computerChoice == "rock"){
return `You Lose ${computerChoice} beats ${playerChoice}`;
}else{
return `You Win! ${playerChoice} beats ${computerChoice}`;
}
}
function incrimentCounter(result){
if(result.substring(0,8) == "You Lose"){
compScore.textContent = Number(compScore.textContent) + 1;
}else if (result.substring(0,7) == "You Win") {
playerScore.textContent = Number(playerScore.textContent) + 1;
}
}
// add counter incrementation after playing game
// Dom Elements
const container = document.querySelector("#container");
const output = document.createElement("p");
container.appendChild(output);
const rock = document.querySelector("#rock");
const paper = document.querySelector("#paper");
const scissors = document.querySelector("#scissors")
const compScore = document.querySelector("#compScore");
const playerScore = document.querySelector("#playerScore");
// Event Listeners
rock.addEventListener("click",() => {
let result= rPSRound(getComputerChoice(),"rock");
incrimentCounter(result);
output.textContent = result;
})
paper.addEventListener("click",() => {
result = rPSRound(getComputerChoice(),"paper");
incrimentCounter(result);
output.textContent = result;
})
scissors.addEventListener("click",() => {
result = rPSRound(getComputerChoice(),"scissors");
incrimentCounter(result);
output.textContent = result;
})