-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
97 lines (74 loc) · 2.41 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let vs = document.querySelector('#myDraw')
let comp = document.querySelector('#compDraw')
let rock = document.getElementById('rock')
let paper = document.getElementById('paper')
let scissors = document.getElementById('scissors')
let result = document.getElementById('VS')
let myWins = document.getElementById('myWins')
let compWins = document.getElementById('compWins')
let me
let computer
let myScore = 0;
let compScore = 0;
function randomNess(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let userClick = choice => {
me = choice
let img = document.createElement('img')
if (scissors) {
img.src = `${choice}.png`
}
vs.appendChild(img)
compPick();
results();
setUp();
}
let compPick = () => {
let img = document.createElement('img')
comp.appendChild(img)
let throws = ['rock', 'paper', 'scissors']
let computerNumber = randomNess(0,2)
computer = throws[computerNumber]
img.src = `${computer}.png`
}
scissors.addEventListener('click', image => {
userClick('scissors')
})
rock.addEventListener('click', image => {
userClick('rock')
})
paper.addEventListener('click', image => {
userClick('paper')
})
let results = () => {
if ( (me === "rock" && computer === "rock") || (me === "paper" && computer === "paper") || (me === "scissors" && computer === "scissors") ) {
setTimeout(() => {
result.textContent = "Tie Game!!";
result.className += "tie";
}, 400);
}
if ( (me === "paper" && computer === "rock") || (me === "rock" && computer === "scissors") || (me === "scissors" && computer === "paper") ) {
setTimeout(() => {
result.textContent = "You Win!!";
myScore += 1;
result.className += "win"; myWins.textContent = myScore;
}, 400);
}
if ( (me === "scissors" && computer === "rock") || (me === "rock" && computer === "paper") || (me === "paper" && computer === "scissors") ) {
setTimeout(() => {
result.textContent = "Computer Wins!!";
compScore += 1;
result.className += "lose"; compWins.textContent = compScore;
}, 400);
}
}
let setUp = () =>
setTimeout(() =>{
result.className = '';
result.textContent = 'VS';
vs.innerHTML = '';
comp.innerHTML = '';
}, 1600);