-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (59 loc) · 1.93 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
let userScore = 0;
let compScore = 0;
const choices = document.querySelectorAll(".choice");
const msg = document.querySelector("#msg")
const userScorepara = document.querySelector("#user-score")
const compScorepara = document.querySelector("#comp-score")
const genCompChoice = ()=> {
const options = ["rock", "paper", "scissors" ]
const ranIdx = Math.floor(Math.random()*3);
return options[ranIdx];
}
const drawGame = ()=> {
console.log("game was draw")
msg.innerText = "Game was draw. play again.";
msg.style.background = "grey"
}
const showWinner = (userWin)=> {
if (userWin){
userScore++;
userScorepara.innerText = userScore;
console.log("user won");
msg.innerText = "you win!";
msg.style.background = "green"
}
else{
compScore++;
compScorepara.innerText = compScore;
console.log("computer won");
msg.innerText = "computer win";
msg.style.background = "red"
}
}
const playGame =(userChoice) => {
console.log("user choice = ",userChoice);
const compChoice = genCompChoice();
console.log("comp choice =",compChoice)
if (userChoice === compChoice) {
drawGame();
}
else {
let userWin = true;
if (userChoice === "rock"){
//comChoice = scissor,paper
userWin = compChoice ==="paper" ? false:true ;
}
else if(userChoice ==="paper"){
//comChoice = rock,paper
userWin = compChoice === "scissors" ? false: true;
}else {
userWin= compChoice === "rock" ? false: true;
}
showWinner(userWin);
}
}
choices.forEach((choice)=>{choice.addEventListener("click", ()=>{
const userChoice = choice.getAttribute("id")
playGame(userChoice);
})
})