samedi 9 novembre 2019

Rock, Paper, Scissors Game shows both win and lose messages

I'm currently working through a Rock, Paper, Scissors project that plays out in the console (this is for The Odin Project, front end coming soon).

Here is what my script looks like for reference:

<script>
    function computerPlay() {
      const choice = ["Rock", "Paper", "Scissors"]

      return choice[Math.floor(Math.random() * choice.length)]
    }

    function play(playerSelection, computerSelection) {
       const lose = console.log('You lose! ' + computerSelection + ' beats ' + playerSelection + '!')
       const win = console.log('You win! ' + playerSelection + ' beats ' + computerSelection + '!')

      if (playerSelection === computerSelection) {
        return console.log("It's a draw! Try again!")
      }

      if (playerSelection === "rock" && computerSelection === "Paper") {
        return lose
      }

      if (playerSelection === "rock" && computerSelection === "Scissors") {
        return win
      }

      if (playerSelection === "paper" && computerSelection === "Scissors") {
        return lose
      }

      if (playerSelection === "paper" && computerSelection === "Rock") {
        return win
      }

      if (playerSelection === "scissors" && computerSelection === "Rock") {
        return lose
      }

      if (playerSelection === "scissors" && computerSelection === "Paper") {
        return win
      }
    }

    function game() {
      playerSelect = prompt("Welcome to Rock, Paper, Scissors! Which one do you choose? \n")
      compSelect = computerPlay()

      console.log("Player chose " + playerSelect)
      console.log("Computer chose " + compSelect)
      console.log(play(playerSelect, compSelect))
    }
  </script>

Right now my output is showing both the win and lose conditions for any given choice like this:

Player chose rock
Computer chose Rock
You lose! Rock beats rock!
You win! rock beats Rock!

I chose to keep the win and lose messages in variables but I know I probably screwed some small detail up there. I've tried adding/removing the if else statements but both messages still show up no matter what choice.

I'm planning on making either choice case insensitive until I solve this error.

Any help would be appreciated, thanks!:

Aucun commentaire:

Enregistrer un commentaire