skip to Main Content

I don’t know where my mistake is. The console gives me the following output "you chose scissor. the computer chose stone. you won!"

const playerChoice = ["scissor", "stone", "paper"];

const computerChoice = function() {
  let randomNumber = Math.floor(Math.random() * playerChoice.length);

  if (randomNumber === 0) {
    return "scissor";
  }
  if (randomNumber === 1) {
    return "stone";
  }
  if (randomNumber === 2) {
    return "paper";
  }
};

function game() {
  const computerSelectedChoice = computerChoice();
  if (playerChoice[0] === computerSelectedChoice) {
    return "its a draw!";
  }
  if (playerChoice[0] && computerSelectedChoice === "stone") {
    return "the computer won!";
  }
  if (playerChoice[0] && computerSelectedChoice === "paper") {
    return "you won!";
  }
  if (playerChoice[1] && computerSelectedChoice === "paper") {
    return "you won!";
  }
  if (playerChoice[1] && computerSelectedChoice === "paper") {
    return "the computer won!";
  }
  if (playerChoice[2] && computerSelectedChoice === "scissor") {
    return "the computer won!";
  }
  if (playerChoice[2] && computerSelectedChoice === "stone") {
    return "you won!";
  }
}

console.log(`you chose ${playerChoice[0]}.`);
console.log(`the computer chose ${computerChoice()}.`);
console.log(game());

I have made some changes, I know the code could be simplified or a switch statement could be implemented. Since I am still a beginner, I have left the code as it is and focused on bug fixing.

2

Answers


  1. I see a couple improvements/fixes that can make so let’s break it down:

    1. We are calling your computerChoice() helper twice: Once for the display and once for the comparison. But this will cause a different result for the display vs the comparison due to it being random. So let’s call it once and store the outcome in variable.
    2. In our condition you are using playerChoice[0] && computerSelectedChoice === "stone" but this just checking if playerChoice[0] is truthy and not necessarily comparing it to the computers choice as intended.
    3. We have a couple duplicate checks we need to remove. Such as: if (playerChoice[1] && computerSelectedChoice === "paper")

    Here is an example of how we can fix these issues:

    const playerChoice = ["scissor", "stone", "paper"];
    const playerSelectedChoice = playerChoice[0];
    
    const computerChoice = function () {
      let randomNumber = Math.floor(Math.random() * playerChoice.length);
      return playerChoice[randomNumber];
    };
    
    function game(player, computer) {
      if (player === computer) {
        return "it's a draw!";
      }
    
      if (
        (player === "scissor" && computer === "stone") ||
        (player === "stone" && computer === "paper") ||
        (player === "paper" && computer === "scissor")
      ) {
        return "the computer won!";
      } else {
        return "you won!";
      }
    }
    
    const computerSelectedChoice = computerChoice();
    console.log(`you chose ${playerSelectedChoice}.`);
    console.log(`the computer chose ${computerSelectedChoice}.`);
    console.log(game(playerSelectedChoice, computerSelectedChoice));
    
    Login or Signup to reply.
  2. This is how I would have done it.

    // to avoid "magic numbers"
    const CHOICES = ['paper', 'scissors', 'rock'];
    const RESULT_TIE = 'Tie';
    const RESULT_WIN = 'You win';
    const RESULT_LOSE = 'You lose';
    
    // what every player object looks like
    class Player {
      constructor(played) {
        this.played = played;
      }
    }
    
    // every game consists of choices and an ability to play it
    class Game {
      constructor() {
        this.choices = CHOICES;
      }
    
      play(p1, p2) {
        const res = (p1.played - p2.played + CHOICES.length) % CHOICES.length;
        const resultText = res === 0 ? RESULT_TIE : res === 1 ? RESULT_WIN : RESULT_LOSE;
    
        this.printResult(resultText, p1.played, p2.played);
      }
    
      // separate method to print the results
      printResult(resultText, p1Choice, p2Choice) {
        console.log(`${resultText}nP1: ${CHOICES[p1Choice]}  P2: ${CHOICES[p2Choice]}`);
      }
    }
    
    const game = new Game();
    const p1 = new Player(Math.floor(Math.random() * CHOICES.length));
    const p2 = new Player(Math.floor(Math.random() * CHOICES.length));
    game.play(p1, p2);
    

    All wins are +1 difference. In order to avoid negative values I add the number of choices and use modulas.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search