Q: How can I fix this rock, paper, scissor game in JavaScript? The first function getComputerChoice() returns a random - rock, paper, or scissor. However, in the playRound function when the computer' choice and user's choice are compared, it shows incorrect answers for a few of them. For example, when I choose rock and computer chooses paper, I win, which is wrong.
I tried using if else and switch statements but faced the same problem. It would be great if someone could help me out.
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();

function getComputerChoice(){
       const randomNumber = Math.floor(Math.random()*3);
       if(randomNumber === 0){
            choice = "rock";
       } else if(randomNumber === 1){
            choice = "paper";
       } else if(randomNumber === 2){
            choice = "scissor";
       } return choice;

}



function playRound(playerSelection, computerSelection){
    let result = '';
    if(computerSelection === playerSelection){
        result = "It's a tie";
    } else if(computerSelection === 'paper' && playerSelection === 'rock'){
        result = "you lose";
    } else if(computerSelection === 'scissor' && playerSelection === 'rock'){
        result = "you win";
    } else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
        result = "You win";
    } else if((computerSelection === 'scissor') && (playerSelection === 'paper')){
        result = "you lose";
    } else if ((computerSelection === 'paper') && (playerSelection === 'scissor') ){
        result = "You win";
    } else if((computerSelection === 'rock') && (playerSelection === 'scissor')){
        result = "You lose";
    }

    return result;
};

alert("The computer chose: " + getComputerChoice());

alert("That means " + playRound(playerSelection, computerSelection));



A: In your first statement, you choose the computer selection.  But in your second to last statement, you call getComputerChoice() which makes a DIFFERENT selection.  Change the next to the last line to
alert("The computer chose: " + computerSelection);


A: Try adding this debug line that I have put in, and then look at the console in the browser.  You will see what the computer really chose
function getComputerChoice(){
       const randomNumber = Math.floor(Math.random()*3);
       if(randomNumber === 0){
            choice = "rock";
       } else if(randomNumber === 1){
            choice = "paper";
       } else if(randomNumber === 2){
            choice = "scissor";
       }
       // debug line
       console.log('randomly chose', choice);
       return choice;

}


A: ChatGPT?
This code will run but it has a couple of problems:
In the getComputerChoice function, the variable choice is declared without const, let, or var. This means that it's a global variable and could cause issues in the future.
The line alert("The computer chose: " + getComputerChoice()); is calling the getComputerChoice function twice, but it should only be called once and stored in the computerSelection variable.
The result of the playRound function is not being logged to the console or displayed in any other way, it's only being returned.
To resolve these problems, you could modify the code as follows:
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();

function getComputerChoice(){
    let choice = '';
    const randomNumber = Math.floor(Math.random() * 3);
    if(randomNumber === 0){
        choice = "rock";
    } else if(randomNumber === 1){
        choice = "paper";
    } else if(randomNumber === 2){
        choice = "scissor";
    }
    return choice;
}

function playRound(playerSelection, computerSelection){
    let result = '';
    if(computerSelection === playerSelection){
        result = "It's a tie";
    } else if(computerSelection === 'paper' && playerSelection === 'rock'){
        result = "you lose";
    } else if(computerSelection === 'scissor' && playerSelection === 'rock'){
        result = "you win";
    } else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
        result = "You win";
    } else if((computerSelection === 'scissor') && (playerSelection === 'paper')){
        result = "you lose";
    } else if ((computerSelection === 'paper') && (playerSelection === 'scissor') ){
        result = "You win";
    } else if((computerSelection === 'rock') && (playerSelection === 'scissor')){
        result = "You lose";
    }

    return result;
};

console.log("The computer chose: " + computerSelection);

console.log("That means " + playRound(playerSelection, computerSelection));


A: Your Code is not consistent : here is the answer:
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();

function getComputerChoice(){
   const randomNumber = Math.floor(Math.random() * 3);
   let choice;
   if(randomNumber === 0){
        choice = "rock";
   } else if(randomNumber === 1){
        choice = "paper";
   } else if(randomNumber === 2){
        choice = "scissors";
   }
   return choice;
}


function playRound(playerSelection, computerSelection){
    let result = '';
    if(computerSelection === playerSelection){
        result = "It's a tie";
    } else if(computerSelection === 'paper' && playerSelection === 'rock'){
        result = "You lose";
    } else if(computerSelection === 'scissors' && playerSelection === 'rock'){
        result = "You win";
    } else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
        result = "You win";
    } else if((computerSelection === 'scissors') && (playerSelection === 'paper')){
        result = "You lose";
    } else if ((computerSelection === 'paper') && (playerSelection === 'scissors') ){
        result = "You lose";
    } else if((computerSelection === 'rock') && (playerSelection === 'scissors')){
        result = "You win";
    }

    return result;
};

let result = playRound(playerSelection, computerSelection);
alert("The computer chose: " + computerSelection);
alert("That means " + result);

Note:

*

*scissor has been corrected to scissors.

*Consistent use of capitalization for "You win" and "You lose".

*The result ofplayRound() function has been stored in a variable
result before   using it in the alert

.
