Help with rock, paper, scissors in console :)

hello, i was wondering if anyone would be able to help me with the rock, paper, scissors project? it would be much appreciated!

i’m a complete newbie so please don’t judge the code too much, i know it’s probably super messy and probably a few errors too!

i think i almost got this working… but it feels like something isn’t quite right when i try to play the game in the console. the computer’s choice only gets stated in the first round which i can’t work out how to fix…

thanks in advance! :slightly_smiling_face:

  <script>

    function computerChoice() {
        let possibleOutcomes = ['rock', 'paper', 'scissors'];
        let randomAnswer = Math.floor(Math.random()*possibleOutcomes.length);
        return possibleOutcomes[randomAnswer].toUpperCase();
    }

    function playerChoice() {
      let playerChoice = prompt('rock, paper or scissors? ');
      return playerChoice.toLowerCase();
    }

    let playerSelection = playerChoice();
    let computerSelection = computerChoice();

    console.log("Player selection", playerSelection);
    console.log("Computer selection", computerSelection);

    function playRound (playerSelection, computerSelection){
        let player = playerSelection.toLowerCase();
            if(playerSelection == computerSelection){
                  return ("it\'s a tie!")
            } else if (playerSelection == "rock") {
                if (computerSelection === "scissors")
                {return ("you win. rock beats scissors.")} 
                else {return ("you lose. paper beats rock.")}
            } else if (playerSelection == "scissors") {
                if (computerSelection === "paper")
                {return ("you win. scissors beats paper.")} 
                else {return ("you lose. rock beats scissors.")}
            } else if (playerSelection == "paper") {
                if (computerSelection === "rock")
                {return ("you win. Ppper beats rock.")} 
                else {return ("you lose. scissors beats paper.")}
            }
        }


    //function plays 5 round game

    const game = () => {
            for (let i = 0; i < 5; i++) {
                playerSelection = prompt('rock, paper or scissors? ')
                console.log(playRound(playerSelection, computerSelection))
            }
        }
        game()

Only declare the variables globally:

// declare playerSelection
var playerSelection;
// declare computerSelection
var computerSelection;

And define them within the scope of the game() function (also, delete the log statements of ‘Player selection’ and ‘Computer selection’ and move them here):

const game = () => {
  for (let i = 0; i < 5; i++) {
    
    // define playerSelection   
    playerSelection = prompt('rock, paper or scissors? ')
    // define computerSelection
    computerSelection =  computerChoice();

    console.log("Player selection", playerSelection);
    console.log("Computer selection", computerSelection); 
    console.log(playRound(playerSelection, computerSelection))
  }
}

This worked for me. As an advice: start indenting your code. It will make it way more readable and more users will be willing to help you, as it will be easier to go through it :slightly_smiling_face:

Hope this helps!

thank you so much! i’ve changed those and it has worked :slight_smile: that makes much more sense now.