'I am making a rock, paper, scissors game and the values of the outputs are not all corect

I typed the statements and everything is working fine only when I try the console with the answer of rock. When I try to see other logs(paper or scissors) it gives me the output based on the first 3 if conditions(from the rock) and the others for some reason doesn't apply.

//My "choice"// 
function playerPlay() {
  let Answer = prompt(`Do you choose Rock,Paper or Scissors ?`).toLowerCase();
  console.log(Answer);
}
playerPlay()
//Computer random choice//
function computerPlay() {
  computerPlay = Math.floor(Math.random() * 3)
  if (computerPlay === 0) {
    console.log("rock");
  } else if (computerPlay === 1) {
    console.log("paper");
  } else if (computerPlay === 2) {
    console.log("scissors");
  }
}
computerPlay()

//The conditions for the game//

function rules() {
  if (Answer = "rock" && computerPlay === 0) {
    return ("Draw");
  } else if (Answer = "rock" && computerPlay === 1) {
    return ("You lost");
  } else if (Answer = "rock" && computerPlay === 2) {
    return ("You won");
  }

  if (Answer = "paper" && computerPlay1 === 0) {
    return ("You Won!");
  } else if (Answer = "paper" && computerPlay === 1) {
    return ("Draw");
  } else if (Answer = "paper" && computerPlay === 2) {
    return ("You Lost!");

  }
  if (Answer = "scissors" && computerPlay === 0) {
    return ("You Lost!")
  } else if (Answer = "scissors" && computerPlay === 1) {
    return ("You Won!");

  } else if (Answer = "scissors" && computerPlay === 2) {
    return ("Draw!");

  }
}
rules()

In the Image you can see that paper with paper is supposed to be a tie but the output actually is "You lost!"



Solution 1:[1]

answer = "..." should be answer == "..." in your if statements. = is used for assignment not for checking equality

You are defining the answer variable inside of your function using let. This means that the answer is only defined within the scope of the function and is not a global variable. Instead you should have it return answer, and instead of calling playerPlay() you should do answer = playerPlay()

The same issue exists with your computer play function. Your rules function returns the answer but nothing prints it out.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1