|
| 1 | +const playerDisplay = document.querySelector('.player-choice'); |
| 2 | +const computerDisplay = document.querySelector('.computer-choice'); |
| 3 | +const resultDisplay = document.querySelector('.result-display'); |
| 4 | +const resultRow = document.querySelector('.result-row'); |
| 5 | +const choices = ['Rock', 'Paper', 'Scissors']; |
| 6 | +let humanScore = 0; |
| 7 | +let computerScore = 0; |
| 8 | + |
| 9 | +// Get computer choice |
| 10 | + |
| 11 | +function getComputerChoice() { |
| 12 | + const computerSelection = choices[Math.floor(Math.random() * choices.length)]; |
| 13 | + return computerSelection; |
| 14 | +} |
| 15 | + |
| 16 | +// Play round |
| 17 | + |
| 18 | +function playRound(humanSelection) { |
| 19 | + const computerSelection = getComputerChoice(); |
| 20 | + |
| 21 | + if (humanSelection === computerSelection) { |
| 22 | + resultRow.textContent = `${humanSelection + ' draws ' + computerSelection}`; |
| 23 | + resultDisplay.textContent = "It's a tie!"; |
| 24 | + } else if ( |
| 25 | + (humanSelection === 'Rock' && computerSelection === 'Scissors') || |
| 26 | + (humanSelection === 'Paper' && computerSelection === 'Rock') || |
| 27 | + (humanSelection === 'Scissors' && computerSelection === 'Paper') |
| 28 | + ) { |
| 29 | + humanScore++; |
| 30 | + playerDisplay.textContent = `USER: ${humanScore}`; |
| 31 | + resultRow.textContent = `${humanSelection + ' beats ' + computerSelection}`; |
| 32 | + resultDisplay.textContent = 'You win!'; |
| 33 | + } else { |
| 34 | + computerScore++; |
| 35 | + computerDisplay.textContent = `COMP: ${computerScore}`; |
| 36 | + resultRow.textContent = `${computerSelection + ' beats ' + humanSelection}`; |
| 37 | + resultDisplay.textContent = 'You lose!'; |
| 38 | + } |
| 39 | + if (humanScore === 5 || computerScore === 5) |
| 40 | + resultDisplay.textContent = 'Game Over!'; |
| 41 | + |
| 42 | + changeColor(resultDisplay); |
| 43 | +} |
| 44 | + |
| 45 | +// Change color of result |
| 46 | + |
| 47 | +function changeColor(element) { |
| 48 | + if (element.textContent === "It's a tie!") { |
| 49 | + element.style.backgroundColor = '#5E81AC'; |
| 50 | + } else if (element.textContent === 'You win!') { |
| 51 | + element.style.backgroundColor = '#A3BE8C'; |
| 52 | + } else if (element.textContent === 'You lose!') { |
| 53 | + element.style.backgroundColor = '#BF616A'; |
| 54 | + } else { |
| 55 | + element.style.backgroundColor = '#2e3440'; |
| 56 | + } |
| 57 | +} |
0 commit comments