-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
73 lines (65 loc) · 2.13 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
let userscore=0;
let compscore=0;
const choices=document.querySelectorAll(".choice");
let msg =document.querySelector("#msg");
let dcomscore=document.querySelector("#computerscore");
let duserscore=document.querySelector("#usescore");
console.log(msg.textContent);
// msg.textContent="new";
const drawGame=()=>{
console.log("this was a draw");
}
const getcompChoice=()=>{
//rock paper scissors
let options=[ "rock", "paper","scissor"]
const randIdx=Math.floor(Math.random()*3);
return options[randIdx];
}
const playGame=(userChoice)=>{
console.log("user choice = " ,userChoice);
//generate comp choice
const compChoice=getcompChoice();
console.log("cmp choice is ", compChoice);
if (userChoice===compChoice) {
drawGame();
msg.textContent="this was a draw";
}else{
if (userChoice==="rock") {
if (compChoice!=="paper") {
userscore=userscore+1;
msg.textContent="you won!";
}else{
compscore=compscore+1;
msg.textContent=" opps! you lost";
}
}else if (userChoice==="paper") {
if (compChoice!=="scissor") {
userscore=userscore+1;
msg.textContent="you won!";
}else{
compscore=compscore+1;
msg.textContent=" opps! you lost";
}
}else if (userChoice==="scissor") {
if (compChoice!=="rock") {
userscore=userscore+1;
msg.textContent="you won!";
}else{
compscore=compscore+1;
msg.textContent=" opps! you lost";
}
}
}
console.log("user score",userscore);
console.log("com score",compscore);
dcomscore.textContent=compscore;
duserscore.textContent=userscore;
}
choices.forEach((choice)=>{
console.log(choice);
choice.addEventListener("click",()=>{
const userChoice=choice.getAttribute("id")
console.log("choice was clicked",userChoice);
playGame(userChoice);
})
})