Rock Paper Scissors Project - console stage - Game Function

Hey everybody, I’m stuck at the end of my Rock Paper Scissors game project.

I’m stuck trying to make the whole thing repeat update the score.

My code will prompt for input, compare it to the computer’s choice, and return results:

  <script>

    //User enters choice
    let userPick = prompt("Rock, Paper, or Scissors?");
    let userHand = userPick.toLowerCase();

    let round = 0;
    let userScore = 0;
    let compScore = 0;
    let computerHand = computerPlay();

    // Computer chooses hand to play
    function computerPlay () {
      let randHand = Math.floor(Math.random() * Math.floor(3))
        if (randHand == 0) {
        return "rock"
      } else if (randHand == 1) {
        return "paper"
      } else if (randHand == 2) {
        return "scissors"
      }
    }

    //Compares hands and provides results
        
    function newRound () {
      
        let uWin = "You win! The score is Player "
        let compWin = "Computer wins! The score is Player "
        let tie = "It's a tie! The score is Player "

        if (userHand == "rock" && computerHand == "scissors") {
        round++;
        userScore++;
        return uWin + userScore + " Computer " + compScore
      } else if (userHand == "rock" && computerHand == "paper") {
        round++;
        compScore++;
        return compWin + userScore + " Computer " + compScore
      } else if (userHand == "paper" && computerHand == "scissors") {
        round++;
        compScore++;
        return compWin + userScore + " Computer " + compScore
      } else if (userHand == "paper" && computerHand == "rock") {
        round++;
        userScore++;
        return uWin + userScore + " Computer " + compScore
      } else if (userHand == "scissors" && computerHand == "paper") {
        round++;
        userScore++;
        return uWin + userScore + " Computer " + compScore
      } else if (userHand == "scissors" && computerHand == "rock") {
        round++;
        compScore++;
        return compWin + userScore + " Computer " + compScore
      } else if (userHand == computerHand) {
        round++;
        return tie + userScore + " Computer " + compScore
      } else {
        return "No, start over and choose: Rock, Paper, or Scissors?"
      }
    }

    console.log("You chose " + userHand + " and the computer chose " + computerHand);
    console.log(newRound(userHand, computerHand))


  </script>



I’ve tried this as the game() function:

function game()  {
     for (i = 0; i < 15; i++)  {
          let userPick = prompt("Rock, Paper, or Scissors?");
          console.log("You chose " + userHand + " and the computer chose " + computerHand);
          console.log(newRound(userHand, computerHand))
          }   
    }
    game();

Which will bring the prompt back up after the user enters their choice, but shows no output in the console for the first entry, then on the rest of the entries it copies the same hands and updates the score as if the same results happen every time, no matter what is written in the prompt.

Totally stumped, been at this project forever. Took most of today to figure out that I can’t figure it out.

Thanks for reading!

Hi,
The problem can be easy resolved by a loop but as far as I remember the project was before the lesson about the loop. I remember having the same problem as you - how to make it to repeat, I solved it with a loop copying someone else’s code but came back to the project when I knew a bit more and did it properly.
The key here is to check the number of rounds after each round.
I did some changes for you, hope that helps.

  <script>
  let round = 0;
  let userScore = 0;
  let compScore = 0;

  function game() {
    if (round < 5) {
      let userPick = prompt('Rock, Paper, or Scissors?');
      let userHand = userPick.toLowerCase();
      let computerHand = computerPlay();
      console.log('You chose ' + userHand + ' and the computer chose ' + computerHand);
      newRound(userHand, computerHand);
    } else {
      console.log('game over');
    }
  }

  // Computer chooses hand to play
  function computerPlay() {
    let randHand = Math.floor(Math.random() * Math.floor(3));
    if (randHand == 0) {
      return 'rock';
    } else if (randHand == 1) {
      return 'paper';
    } else if (randHand == 2) {
      return 'scissors';
    }
  }

  //Compares hands and provides results

  function newRound(argument1, argument2) {
    let uWin = 'You win! The score is Player ';
    let compWin = 'Computer wins! The score is Player ';
    let tie = "It's a tie! The score is Player ";

    if (argument1 == 'rock' && argument2 == 'scissors') {
      round++;
      userScore++;

      console.log(uWin + userScore + ' Computer ' + compScore);
    } else if (argument1 == 'rock' && argument2 == 'paper') {
      round++;
      compScore++;

      console.log(compWin + userScore + ' Computer ' + compScore);
    } else if (argument1 == 'paper' && argument2 == 'scissors') {
      round++;
      compScore++;

      console.log(compWin + userScore + ' Computer ' + compScore);
    } else if (argument1 == 'paper' && argument2 == 'rock') {
      round++;
      userScore++;

      console.log(uWin + userScore + ' Computer ' + compScore);
    } else if (argument1 == 'scissors' && argument2 == 'paper') {
      round++;
      userScore++;

      console.log(uWin + userScore + ' Computer ' + compScore);
    } else if (argument1 == 'scissors' && argument2 == 'rock') {
      round++;
      compScore++;

      console.log(compWin + userScore + ' Computer ' + compScore);
    } else if (argument1 == argument2) {
      round++;

      console.log(tie + userScore + ' Computer ' + compScore);
    } else {
      console.log('No, start over and choose: Rock, Paper, or Scissors?');
    }

    //call the game() function to check if the rounds are below the number you want
    game();
  }

  //Initiate the game on load
  game();
</script>

Let me know if something is unclear, good luck :slight_smile:

Wow, thanks Bojo!

That works perfectly, and I can see a few ways that I was tripping myself up now. It’s way less satisfying to ask for help, but I was stuck forever.

One question:

  • Where we created newRound() , you entered argument1 & argument2 as the parameters. And when you called newRound() within game(), you entered the parameters (userHand, computerHand) it should substitute in as argument1 & 2.

Is this required? As opposed to entering userHand & computerHand as the parameters when we create newRound()? Or just a “best practices” thing?

When you declared the newRound function you did it with no parameters, which is absolutely fine if you were not calling the function after that with parameters newRound(userHand, computerHand), if you did call it as newRound() (no parameters) than it will work just fine as the arguments you passed are variables declared outside the function. Long story short if you declare a function with no parameters call it later the same way or make a function with parameter(s) and pass it any argument(s) as parameter(s) you want when calling it.

        function myFunction(a, b) {
          return a * b;
        }

myFunction(2,3)
will return 6
We pass 2 and 3 as arguments of a and b

Or

let a = 2;
let b = 3;
function myFunction() {
return a * b;
}

myFunction()
no parameters - the function works with global variables
will return 6

That makes sense. Fantastic! Thank you Bojo!