Professional Documents
Culture Documents
SGB Sloth Tree Spider Js
SGB Sloth Tree Spider Js
selectionButtons.forEach(selectionButton => {
selectionButton.addEventListener('click', e => {
const selectionName = selectionButton.dataset.selection
const selection = SELECTIONS.find(selection => selection.name ===
selectionName)
makeSelection(selection)
})
})
function makeSelection(selection) {
const computerSelection = randomSelection()
const yourWinner = isWinner(selection, computerSelection)
const computerWinner = isWinner(computerSelection, selection)
addSelectionResult(computerSelection, computerWinner)
addSelectionResult(selection, yourWinner)
if (yourWinner) incrementScore(yourScoreSpan)
if (computerWinner) incrementScore(computerScoreSpan)
}
function incrementScore(scoreSpan) {
scoreSpan.innerText = parseInt(scoreSpan.innerText) + 1
}
function randomSelection() {
const randomIndex = Math.floor(Math.random() * SELECTIONS.length)
return SELECTIONS[randomIndex]
}