skip to Main Content

I am trying to build rock paper scissor game. In this for every condition it is showing computer as winner and only the computer score is increasing. In playRound function the conditions are correct i am not able to rectify the problem here. Plz help in finding my mistake.

const choices = ["rock", "paper", "scissors"];
let computerScore = 0;
let playerScore = 0;
var computer;

function computerChoice() {
    const computerId = document.getElementById('computerid');
    computer = choices[Math.floor(Math.random() * choices.length)];
    computerId.innerHTML = computer;
    console.log(computer);
    return computer;
}

function playRound(computerSel, playerSel) {
    return (computerSel === playerSel) ? "It's a Tie" :
        (computerSel === "rock" && playerSel === "scissors") ||
        (computerSel === "paper" && playerSel === "rock") ||
        (computerSel === "scissors" && playerSel === "paper") ? "Computer" : "Player";
    }

var playerSelection;
var computerSelection;
function playGame() {
    const buttons = document.querySelectorAll('button');
    buttons.forEach(btn => {
        btn.addEventListener('click', (e) => {
             playerSelection = e.target.textContent;
             console.log(playerSelection);
            const playerId = document.getElementById('playerid');
            playerId.innerHTML = playerSelection;
             computerSelection = computerChoice();
            const result = playRound(computerSelection, playerSelection);
            console.log(result);
            updateScore(result);
            
        });
    });
}

function updateScore(result) {
    const playerscore = document.getElementById('playerscore');
    const computerscore = document.getElementById('computerscore');
   
    if (result === "Player") {
        playerScore++;
    } else if (result === "Computer") {
        computerScore++;
    }
    playerscore.innerText = playerScore;
    computerscore.innerText = computerScore;
    if (playerScore === 5 || computerScore === 5) {
        endGame();
    }
}

function endGame() {
    const winner = (playerScore > computerScore) ? "Player" : "Computer";
    console.log(`The winner is ${winner}`);
}

playGame();

2

Answers


  1. I think there issue with condition. you have to update the condition in the playRound function.

    function playRound(computerSel, playerSel) {
        if (playerSel === computerSel) {
            return "It's a Tie";
        } else if (
            (computerSel === "rock" && playerSel === "scissor") ||
            (computerSel === "paper" && playerSel === "rock") ||
            (computerSel === "scissor" && playerSel === "paper")
        ) {
            return "Computer";
        } else {
            return "Player"; 
        }
    }
    Login or Signup to reply.
  2. you are checking wrong condition for player win in your else if

    second statement login is wrong just replace "rock" with "scissor" for player win condition.

    else if (
        (computerSel === "rock" && playerSel === "paper") ||
        (computerSel === "paper" && playerSel === "scissor") ||
        (computerSel === "scissor" && playerSel === "rock")
    ) {
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search