-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
83 lines (75 loc) · 2.56 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// jshint esversion:8
const result = document.getElementById('result');
const yourScore = document.querySelector('.your-score');
const computerScore = document.querySelector('.computer-score');
const btn = document.querySelectorAll('.btn');
const welcome = document.querySelector('.welcome');
const userName = document.getElementById('user-name');
const game = document.querySelector('.game-board');
const name = document.getElementById('name-tag');
const form = document.getElementById('form');
const img = document.querySelectorAll('.img');
let yScore = 0;
let cScore = 0;
let computerChoice = '';
form.addEventListener('submit', function(e){
e.preventDefault();
welcome.hidden = true;
name.innerText = userName.value;
game.style.display = "block";
});
const computerOption = () => {
let randomNumber = Math.floor(Math.random() * 3);
if(randomNumber === 0){
computerChoice = 'stone';
} else if(randomNumber === 1){
computerChoice = 'paper';
} else {
computerChoice = 'scissor';
}
return computerChoice;
};
for(let i = 0; i < btn.length; i++){
btn[i].addEventListener('click', function() {
for(let j = 0; j < img.length; j++){
img[j].classList.remove('apply-shake');
}
let cmp = computerOption();
if(cmp == this.id){
for(let j = 0; j < img.length; j++){
if(img[j].classList.contains(cmp)){
img[j].classList.toggle('apply-shake');
}
}
result.innerText = "Tie";
} else if(cmp == 'stone' && this.id == 'paper'){
result.innerText = "Paper covers the stone you win.";
yScore++;
yourScore.innerText = yScore;
} else if(cmp == 'stone' && this.id == 'scissor'){
result.innerText = "Stone break scissor you lose";
cScore++;
computerScore.innerText = cScore;
}
else if(cmp == 'paper' && this.id == 'scissor'){
result.innerText = "Scissor cuts paper you win";
yScore++;
yourScore.innerText = yScore;
}
else if(cmp == 'paper' && this.id == 'stone'){
result.innerText = "paper cover stone you lose";
cScore++;
computerScore.innerText = cScore;
}
else if(cmp == 'scissor' && this.id == 'paper'){
result.innerText = "sciccor cuts paper you lose";
cScore++;
computerScore.innerText = cScore;
}
else if(cmp == 'scissor' && this.id == 'stone'){
result.innerText = "Stone break scissor you win.";
yScore++;
yourScore.innerText = yScore;
}
});
}