Skip to content

Commit

Permalink
Merge pull request #5 from priyanshi-git/Correct-Result
Browse files Browse the repository at this point in the history
Resolved the Bug
  • Loading branch information
uday03meh authored Oct 11, 2022
2 parents 20ce46a + 7230674 commit 5d67fea
Showing 1 changed file with 65 additions and 76 deletions.
141 changes: 65 additions & 76 deletions script.js
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();

0 comments on commit 5d67fea

Please sign in to comment.