-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
61 lines (50 loc) · 1.58 KB
/
javascript.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
function computerPlay() {
var selectionArray = ["rock", "paper", "scissors"];
var selection = selectionArray[Math.floor(Math.random() * 3)];
var extension = ''
if(selection == 'paper') {
extension = '.jfif'
} else if (selection == 'rock') {
extension = '.jpg'
} else {
extension = '.png'
}
const compImg = document.querySelector('#compSelecImg')
compImg.src = `imgs/${selection}${extension}`
return selection;
}
function playRound(e) {
e.target.classList.add('animated')
var playerSelection = e.toElement.id
var computerSelection = computerPlay();
if (playerSelection == computerSelection) {
setMessage("Draw")
} else if (playerSelection == "rock" && computerSelection == "paper" ||
playerSelection == "paper" && computerSelection == "scissors" ||
playerSelection == "scissors" && computerSelection == "rock") {
setMessage("Lose")
computerScore += 1
setScore()
} else {
setMessage("Win")
playerScore += 1
setScore()
}
}
function setScore() {
score.innerHTML = "Score: " + playerScore + " to " + computerScore
}
function setMessage(message) {
query = document.querySelector('.message h3')
query.innerHTML = message
}
function removeTransition(e) {
e.target.classList.remove('animated');
}
const btns = Array.from(document.querySelectorAll('.btn'))
btns.forEach(btn => btn.addEventListener('click', playRound))
btns.forEach(btn => btn.addEventListener('transitionend', removeTransition))
var computerScore = 0
var playerScore = 0
const score = document.querySelector('.score h3')
setScore()