From 72306747264eb79594dbd6088009b1464db7201e Mon Sep 17 00:00:00 2001 From: Priyanshi Goel <82112540+priyanshi-git@users.noreply.github.com> Date: Wed, 12 Oct 2022 01:10:01 +0530 Subject: [PATCH] Resolved the Bug A simple explanation of how the code works now is - If the computer and the player have the same sign then the "Draw" message is shown and the score doesn't get affected at all - If the player wins then "You Win !" message is shown and the score increases - If the player loses then "You Lose !" message is shown and the score decreases p.s. there seems to be a lot of changes but they are only because of formatting changes. --- script.js | 141 +++++++++++++++++++++++++----------------------------- 1 file changed, 65 insertions(+), 76 deletions(-) diff --git a/script.js b/script.js index 10efe54..21e69ff 100644 --- a/script.js +++ b/script.js @@ -1,102 +1,91 @@ - -const choices = ['Rock', 'Paper', 'Scissors']; +const choices = ["Rock", "Paper", "Scissors"]; function getComputerChoice(choices) { - return choices[Math.floor(Math.random() * choices.length)]; + return choices[Math.floor(Math.random() * choices.length)]; } // console.log(getComputerChoice(choices)); function getResult(playerChoice, computerChoice) { - if(playerChoice == 'Rock'){ - if(computerChoice == 'Rock'){ - document.getElementById("player-score").innerText; - } - if(computerChoice == 'Paper'){ - document.getElementById("player-score").innerText--; - } - else{ - document.getElementById("player-score").innerText++; - } + scoreBefore = document.getElementById("player-score").innerText; + if (playerChoice == "Rock") { + if (computerChoice == "Rock") { + //do nothing if tie + } else if (computerChoice == "Paper") { + document.getElementById("player-score").innerText--; + } else { + document.getElementById("player-score").innerText++; } + } - if(playerChoice == 'Scissors'){ - if(computerChoice == 'Rock'){ - document.getElementById("player-score").innerText--; - } - if(computerChoice == 'Paper'){ - document.getElementById("player-score").innerText++; - } - else{ - document.getElementById("player-score").innerText; - } + if (playerChoice == "Scissors") { + if (computerChoice == "Rock") { + document.getElementById("player-score").innerText--; + } else if (computerChoice == "Paper") { + document.getElementById("player-score").innerText++; + } else { + //do nothing if tie } + } - if(playerChoice == 'Paper'){ - if(computerChoice == 'Rock'){ - document.getElementById("player-score").innerText++; - } - if(computerChoice == 'Paper'){ - document.getElementById("player-score").innerText; - } - else{ - document.getElementById("player-score").innerText--; - } + if (playerChoice == "Paper") { + if (computerChoice == "Rock") { + document.getElementById("player-score").innerText++; + } else if (computerChoice == "Paper") { + //do nothing if tie + } else { + document.getElementById("player-score").innerText--; } - score = document.getElementById("player-score").innerText - return score; + } + scoreAfter = document.getElementById("player-score").innerText; + return scoreBefore, scoreAfter; } - -let finalresult = document.getElementById('result').innerText; -function showResult(score, playerChoice, computerChoice) { - document.getElementById('hands').innerText - if(computerChoice == 'Rock'){ - document.getElementById('hands').innerText = ' ✊ '; - } - else if(computerChoice == 'Paper'){ - document.getElementById('hands').innerText = ' 🤚 '; - } - else{ - document.getElementById('hands').innerText = ' ✌ '; - } +let finalresult = document.getElementById("result").innerText; - finalresult = document.getElementById('result').innerText; - if(score == -1){ - document.getElementById('result').innerText = 'You Lose!' - } - else if(score == 1){ -document.getElementById('result').innerText = 'You Won!'; - } - else{ - document.getElementById('result').innerText = 'Draw' - } - +function showResult(scoreBefore, scoreAfter, playerChoice, computerChoice) { + document.getElementById("hands").innerText; + if (computerChoice == "Rock") { + document.getElementById("hands").innerText = " ✊ "; + } else if (computerChoice == "Paper") { + document.getElementById("hands").innerText = " 🤚 "; + } else { + document.getElementById("hands").innerText = " ✌ "; + } + + finalresult = document.getElementById("result").innerText; + if (parseInt(scoreBefore) > parseInt(scoreAfter)) { + document.getElementById("result").innerText = "You Lose!"; + } else if (parseInt(scoreBefore) < parseInt(scoreAfter)) { + document.getElementById("result").innerText = "You Won!"; + } else { + document.getElementById("result").innerText = "Draw"; + } } function onClickRPS(playerChoice) { - computerChoice = getComputerChoice(choices) - getResult(playerChoice,computerChoice); - showResult(score,playerChoice,computerChoice); + computerChoice = getComputerChoice(choices); + getResult(playerChoice, computerChoice); + showResult(scoreBefore, scoreAfter, playerChoice, computerChoice); } - function playGame() { - const rpsButtons = document.querySelectorAll('.rpsButton') + const rpsButtons = document.querySelectorAll(".rpsButton"); - rpsButtons.forEach(rpsButton => rpsButton.onclick=() => { + rpsButtons.forEach( + (rpsButton) => + (rpsButton.onclick = () => { onClickRPS(rpsButton.value); - }) - + }) + ); } function endGame() { - const endGameButton = document.getElementById('endGameButton'); - endGameButton.onclick = () => { - document.getElementById("player-score").innerText = 0; - document.getElementById('result').innerText = ''; - document.getElementById('hands').innerText = '' - } + const endGameButton = document.getElementById("endGameButton"); + endGameButton.onclick = () => { + document.getElementById("player-score").innerText = 0; + document.getElementById("result").innerText = ""; + document.getElementById("hands").innerText = ""; + }; } - -playGame() -endGame() \ No newline at end of file +playGame(); +endGame();