Rock-Paper-Scissors help [SOLVED]

I don’t understand why what I have so far is returning inaccurate results.
example… else if (playerChoice(player) == choice[0] && computerPlay() == choice[0])
{
console.log(“Draw”);
}

sometimes it will return “You Lose” when it’s supposed to say “Draw”. There are more instances where the results come up inaccurate.

Please Help this noob lol

Rock-paper-scissors //make a function called computer play //return the strings rock paper or scissors randomly within the function cplay //write a function that plays a single round of rps and holds two parameters playerselection and computer selection
  const choice = ["rock","paper","scissors"]

  let computerPlay = function () {
    let computerRandom = [Math.floor(
      Math.random() * choice.length)]
    ;
    if (computerRandom == 0) {
      return choice[0];
    } else if (computerRandom == 1) {
      return choice[1];
    } else if (computerRandom == 2) {
      return choice[2];
    } else {
      console.log("error in computerPlay");
    }
  };
  // console.log(computerPlay());

  // if player choice = 'rock' console.log(rock); etc...

  let playerChoice = function (playerSelection) {
    playerSelection = playerSelection.toLowerCase();
    if (playerSelection == "rock") {
      return choice[0];
    } else if (playerSelection == "paper") {
      return choice[1];
    } else {
      return choice[2];
    }
  };
  // console.log(playerChoice('rock'));
  let playRound = function (player) {
    playerChoice(player);
    computerPlay();
    

    if (
      playerChoice(player) == choice[0] &&
      computerPlay() == choice[2]
    ) {
      console.log("You Win!");
    } else if (
      playerChoice(player) == choice[0] &&
      computerPlay() == choice[1]
    ) {
      console.log("You Lose!");
    } else if (
      playerChoice(player) == choice[0] &&
      computerPlay() == choice[0]
    ) {
      console.log("Draw!");
    } else {
      console.log("errorz");
    }

    return playerChoice(player) + "\n" + computerPlay();
  };

  console.log(playRound("rock"));
</script>

Hi probaby its

in the first if you called playerCoice(player) and computerPlay(), but if the argument of the if istn true, then run the else if statement to see the value of the argument, calling another time playerCoice(player) and computerPlay(), changing the results of the round.

To avoid this problem call each function only one time per round and save the results in a variable. You can use these variables to check who won, without changing the results.

Hope this will help.

1 Like

Thank you so much. I will double check later when I get time. I agree that that is probably it. (edit : That was it!)