-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from priyanshi-git/Correct-Result
Resolved the Bug
- Loading branch information
Showing
1 changed file
with
65 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
playGame(); | ||
endGame(); |