diff --git a/Rock-Paper-Scissors-Game/app.js b/Rock-Paper-Scissors-Game/app.js index 81b7535..2222b4f 100644 --- a/Rock-Paper-Scissors-Game/app.js +++ b/Rock-Paper-Scissors-Game/app.js @@ -1,18 +1,25 @@ -const computerChoiceDisplay = document.getElementById('computer-choice'); -const userChoiceDisplay = document.getElementById('user-choice'); -const resultDisplay = document.getElementById('winner'); -const possibleChoices = document.querySelectorAll('.button-container > img'); +const computerChoiceDisplay = document.getElementById("computer-choice"); +const userChoiceDisplay = document.getElementById("user-choice"); +const resultDisplay = document.getElementById("winner"); +const possibleChoices = document.querySelectorAll(".button-container > img"); let userChoice; let computerChoice; let result; +let score = { + wins: 0, + losses: 0, + ties: 0, +}; -possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener('click', (e) => { - userChoice = e.target.alt; - userChoiceDisplay.src = e.target.src; - generateComputerChoice(); - getResult(); -})); +possibleChoices.forEach((possibleChoice) => + possibleChoice.addEventListener("click", (e) => { + userChoice = e.target.alt; + userChoiceDisplay.src = e.target.src; + generateComputerChoice(); + getResult(); + }) +); function generateComputerChoice() { const randomNumber = Math.floor(Math.random() * possibleChoices.length); @@ -27,17 +34,29 @@ function getResult() { resultDisplay.innerHTML = result; resultDisplay.className = "it-s-a-draw"; } else if ( - (computerChoice === 'Rock' && userChoice === 'Paper') || - (computerChoice === 'Scissors' && userChoice === 'Rock') || - (computerChoice === 'Paper' && userChoice === 'Scissors') + (computerChoice === "Rock" && userChoice === "Paper") || + (computerChoice === "Scissors" && userChoice === "Rock") || + (computerChoice === "Paper" && userChoice === "Scissors") ) { - result = 'You win!'; - resultDisplay.innerHTML = ``; - resultDisplay.className = "you-win"; + result = "You win!"; + resultDisplay.innerHTML = ``; + resultDisplay.className = "you-win"; } else { - result = 'You lose!'; - resultDisplay.innerHTML = ``; - resultDisplay.className = "you-lose"; + result = "You lose!"; + resultDisplay.innerHTML = ``; + resultDisplay.className = "you-lose"; } + if (result === "You win!") { + score.wins += 1; + } else if (result === "You lose!") { + score.losses += 1; + } else if (result === "It's a draw") { + score.ties += 1; + } + scoreElement(); +} +function scoreElement() { + document.querySelector( + ".js-score" + ).innerHTML = `Wins: ${score.wins}, Losses: ${score.losses}, Ties: ${score.ties}.`; } - diff --git a/Rock-Paper-Scissors-Game/index.html b/Rock-Paper-Scissors-Game/index.html index 9dc2f07..975214c 100644 --- a/Rock-Paper-Scissors-Game/index.html +++ b/Rock-Paper-Scissors-Game/index.html @@ -1,28 +1,35 @@
- +