-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (55 loc) · 1.8 KB
/
script.js
File metadata and controls
66 lines (55 loc) · 1.8 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
60
61
62
63
64
65
66
const selectionButton = document.querySelectorAll('[data-selection]')
const finalColumn = document.querySelector('[data-final-column]')
const computerscoreSpan= document.querySelector('[data-computer-score]')
const yourscoreSpan= document.querySelector('[data-your-score]')
const SELECTION=[
{
name:'rock',
emoji:'✊',
beats:'scissor'
},
{
name:'paper',
emoji:'🤚',
beats:'rock'
},
{
name:'scissor',
emoji:'✌️',
beats:'paper'
}
]
selectionButton.forEach(selectionButton=>{
selectionButton.addEventListener('click', e =>{
const selectionName=selectionButton.dataset.selection
const selection = SELECTION.find(selection=>selection.name===selectionName)
makeselction(selection)
})
})
function makeselction(selection){
const computerSelection = randomSelection()
const yourWinner= isWinner(selection,computerSelection)
const computerWinner= isWinner(computerSelection,selection)
console.log(computerSelection);
addselectionResult(computerSelection,computerWinner)
addselectionResult(selection,yourWinner)
if (yourWinner) incrementScore(yourscoreSpan)
if (computerWinner) incrementScore(computerscoreSpan)
}
function incrementScore(scoreSpan){
scoreSpan.innerText=parseInt(scoreSpan.innerText) + 1
}
function addselectionResult(selection,winner){
const div=document.createElement('div')
div.innerText=selection.emoji
div.classList.add('result-selection')
if(winner) div.classList.add('winner')
finalColumn.after(div)
}
function isWinner(selection,opponentselection){
return selection.beats===opponentselection.name
}
function randomSelection(){
const randomIndex=Math.floor(Math.random()*SELECTION.length)
return SELECTION[randomIndex]
}