-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (61 loc) · 1.76 KB
/
app.js
File metadata and controls
67 lines (61 loc) · 1.76 KB
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
let userScore=0;
let computerScore=0;
const userScore_span=document.getElementById("user-score");
const computerScore_span=document.getElementById("computer-score");
const scoreboard_div=document.querySelector(".score-board");
const result_h2=document.querySelector(".result >h1");
const rock_div=document.getElementById("r");
const scissors_div=document.getElementById("s");
const paper_div =document.getElementById("p");
function getComputerChoice(){
const choices=['r','p','s'];
const randomNumber=Math.floor(Math.random()*3);
return choices[randomNumber];
}
function win(userChoice,ComputerChoice){
userScore++;
document.getElementById('u1').innerHTML = userScore;
document.getElementById('r1').innerHTML= userChoice + " beat " + ComputerChoice + ". You Win!";
}
function lose(userChoice,ComputerChoice){
computerScore++;
document.getElementById('u2').innerHTML = computerScore;
document.getElementById('r1').innerHTML= ComputerChoice + " beat " + userChoice + ". You Lose!";
}
function draw(userChoice,ComputerChoice){
console.log("U draw");
document.getElementById('u1').innerHTML = userScore;
document.getElementById('r1').innerHTML= ". You draw!...Try Again";
}
function game(userChoice){
const ComputerChoice=getComputerChoice();
switch (userChoice+ComputerChoice){
case "rs":
case "pr":
case "sp":
win(userChoice,ComputerChoice);
break;
case "rp":
case "ps":
case "sr":
lose(userChoice,ComputerChoice);
break;
case "rr":
case "pp":
case "ss":
draw();
break;
}
}
function main(){
rock_div.addEventListener('click',function(){
game("r");
})
paper_div.addEventListener('click',function(){
game("p");
})
scissors_div.addEventListener('click',function(){
game("s");
})
}
main()