-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (59 loc) · 2.22 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
let userScore = 0;
let computerScore = 0;
let userScore_span = document.getElementById("user-score");
let computerScore_span = document.getElementById("computer-score");
let score = document.querySelector(".score");
let result = document.querySelector(".result>p");
const rock = document.getElementById("rock");
const paper = document.getElementById("paper");
const scissor = document.getElementById("scissor");
function wins(userChoice, computerChoice){
console.log("WIN");
userScore++;
console.log("USER- "+userScore);
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result.innerHTML ="YOU WON!";
}
function loses(userChoice, computerChoice){
console.log("LOST");
computerScore++;
console.log("Computer- "+computerScore);
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result.innerHTML = "YOU LOST!";
}
function tie(userChoice, computerChoice){
console.log("TIE");
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result.innerHTML = "IT'S A TIE!";
}
function game(userChoice){
const compChoice = computerChoice();
if(userChoice == "rock" && compChoice == "paper") loses(userChoice, computerChoice);
else if(userChoice == "paper" && compChoice == "scissor") loses(userChoice, computerChoice);
else if(userChoice == "scissor" && compChoice == "rock") loses(userChoice, computerChoice);
else if(userChoice == "scissor" && compChoice == "paper") wins(userChoice, computerChoice);
else if(userChoice == "paper" && compChoice =="rock") wins(userChoice, computerChoice);
else if(userChoice == "rock" && compChoice == "scissor") wins(userChoice, computerChoice);
else if(userChoice == compChoice) tie(userChoice, computerChoice);
}
game();
function computerChoice(){
const choices = ['rock', 'paper', 'scissor'];
const randomNumber = Math.floor(Math.random()*3);
return choices[randomNumber];
}
function main(){
rock.addEventListener('click', function(){
game("rock")
})
paper.addEventListener('click', function(){
game("paper")
})
scissor.addEventListener('click', function(){
game("scissor")
})
}
main();