0

I am attempting to create a rock paper scissors game with vanilla JS. I am getting an incorrect result when pressing the button associated with a choice. The choice itself is correct (if I choose "rock", the console is logging "rock" correctly), but the result is incorrect (if I choose "rock" and the computer chooses "paper" I get anything from "it's a tie" or "you lose" for example. Here's the code:

const gameChoices = ["rock", "paper", "scissors"];

function getComputerChoice() {
  const randomIndex = Math.floor(Math.random() * gameChoices.length);
  const randomChoice = gameChoices[randomIndex];
  return randomChoice;
}

const choiceButton = document.querySelectorAll(".choiceButton");

choiceButton.forEach((button) =>
  button.addEventListener("click", () => {
    playerSelection = button.textContent.toLowerCase();
    console.log(playerSelection);
    computerSelection = getComputerChoice();
    console.log(getComputerChoice());
    console.log(playRound(playerSelection, computerSelection));
  })
);

function playRound(playerSelection, computerSelection) {
  if (playerSelection == "rock" && computerSelection == "paper") {
    ++computerScore;
    console.log("You lose");
  } else if (playerSelection == "rock" && computerSelection == "scissors") {
    console.log("You win");
    ++playerScore;
  } else if (playerSelection == "scissors" && computerSelection == "rock") {
    console.log("You lose");
    ++computerScore;
  } else if (playerSelection == "scissors" && computerSelection == "paper") {
    console.log("You win");
    ++playerScore;
  } else if (playerSelection == "paper" && computerSelection == "scissors") {
    console.log("You lose");
    ++computerScore;
  } else if (playerSelection == "paper" && computerSelection == "rock") {
    console.log("You win");
    ++playerScore;
  } else if (playerSelection == computerSelection) {
    console.log("It's a tie");
  } else {
    return console.log(wrongSelection());
  }
}

function wrongSelection(playerSelection) {
  if ((playerSelection != "rock", "paper", "scissors")) return "try again";
}

I think the code is running the getComputerChoice function multiple times. So it runs it and logs it as one value, but then when I call playRound, it runs getComputerChoice again and produces a different value. Thus making it look like it's not following the logic. I'm not quite sure how to fix this issue though.

  • 2
    Although this isn't overwriting anything, **console.log(getComputerChoice());** isn't showing you the computer's choice, its showing you ANOTHER random option. Try **console.log(computerSelection)** to see the actual value. – imvain2 Nov 15 '22 at 19:45
  • 2
    above is it. you're running the computer choice function and putting it in a variable, then you are running the computer choice function a second time to print. the variable you filled the first time however still has the value from the first time the function was run, so you will have no idea what choice the computer actually made by the print your performing. – dqhendricks Nov 15 '22 at 19:48
  • Also , you need to declare let computerScore = 0, playerScore = 0. And you are consoling playRound but only returning a value in your else. – imvain2 Nov 15 '22 at 19:50
  • That did the trick as far as giving me the correct value. One other random question here though: so the console looks correct in that it's returning the correct playerSelection, computerSelection, and result but it's also returning "undefined" for console.log(playRound(playerSelection, computerSelection)); for some reason. – spyguy92 Nov 15 '22 at 19:50
  • 1
    `(playerSelection != "rock", "paper", "scissors")` makes no sense. Please see [Check variable equality against a list of values](/q/4728144/4642212). – Sebastian Simon Nov 15 '22 at 19:52
  • **undefined** is because you are consoling playRound but don't actually return anything except in your else statement which also needs to be updated. – imvain2 Nov 15 '22 at 19:53
  • I'm unclear on how to fix the issue you are pointing out. – spyguy92 Nov 15 '22 at 20:01
  • change to playRound(playerSelection, computerSelection); not console.log(playRound(playerSelection, computerSelection)); – Bryan Dellinger Nov 15 '22 at 20:08

0 Answers0