-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (64 loc) · 1.81 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
let insertElemnt= document.querySelector('#compare')
let w = document.querySelector('#wins')
let l = document.querySelector('#losses')
let d = document.querySelector('#ties')
let score = JSON.parse(localStorage.getItem('score')) || {
wins:0,
losses:0,
tie:0
}
w.innerHTML=score.wins
l.innerHTML=score.losses
d.innerHTML=score.tie
function cal(x){
let cmp = cp()
let resele = document.querySelector('.result')
resele.scrollIntoView()
console.log(x)
console.log(cmp)
if((x=='rock' && cmp=='scissors') || (x=='paper' && cmp=='rock') || (x=='scissors' && cmp=='paper')){
score.wins++
resele.innerHTML="You Win"
resele.id='result-win'
}
else if(x==cmp){
score.tie++
resele.innerHTML="Tie"
resele.id='result-tie'
}
else{
score.losses++
resele.innerHTML="You Lose"
resele.id='result-lose'
}
insertElemnt.innerHTML = `<p id="left">You</p>
<div id="js-insert"></div>
<img src="images/${x}-emoji.png" alt="Paper" id="top">
<img src="images/${cmp}-emoji.png" alt="Rock">
<p>Computer</p>`
w.innerHTML=score.wins
l.innerHTML=score.losses
d.innerHTML=score.tie
localStorage.setItem('score',JSON.stringify(score))
}
function cp(){
let r = Math.floor(Math.random()*3)+1
if(r==1){
return 'rock'
}
else if(r==2){
return 'paper'
}
else if(r==3){
return 'scissors'
}
}
function reset(){
score.wins=0
score.losses=0
score.tie=0
w.innerHTML=score.wins
l.innerHTML=score.losses
d.innerHTML=score.tie
localStorage.setItem('score',JSON.stringify(score))
}