-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (52 loc) · 1.42 KB
/
script.js
File metadata and controls
59 lines (52 loc) · 1.42 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
const wrapper = document.getElementById("wrapper");
const you = document.getElementById("you");
const machine = document.getElementById("pc");
const result = document.getElementById("result");
const choices = [
{
win: "scissors",
lose: "paper",
name: "rock"
},
{
win: "rock",
lose: "scissors",
name: "paper"
},
{
win: "paper",
lose: "rock",
name: "scissors"
}
]
function machineChoice(){
let random = Math.floor(Math.random() * 3);
return random;
}
wrapper.addEventListener("click", (e) =>{
let element = e.target.value;
let pc = machineChoice();
if(element){
you.innerHTML = element;
machine.innerHTML = choices[pc].name;
if(element == choices[pc].win){
result.innerHTML = "You lost! :("
throwAnimation(choices[pc].name);
}else if(element == choices[pc].lose){
result.innerHTML = "You won! :)"
throwAnimation(element);
}else{
result.innerHTML = "Tie game... :|"
}
}
})
function throwAnimation(winner){
const element = document.getElementById(`${winner}-animation`);
const animation = document.getElementById("animations");
animation.style.zIndex = 2;
element.classList.add(winner);
setTimeout(()=>{
element.classList.remove(winner);
animation.style.zIndex = -1;
}, 4000)
}