Rock-Paper-Scissors Help

Hey folks,

This is my first post here so I wanted to ask for some help. I’ve done a fair bit of searching on Google, StackOverFlow etc but I’m still stuck and would love some guidance on the rock-paper-scissors question in Web Development 101.

I’ve got the playRound() function working fine. This allows the human player to add an input from a prompt, and returns an integer based on the outcome of the game against a computerPlay function that randomly picks the computers choice. I originally returned strings

I now need to take the playRound function and use it within a new game() function, that allows the player to choose a number of games, play that number of games against the computer, and keep score.

I’m stuck on calling the playRound function within the game() function. I want it to add 1 to either playerScore or computerScore based on the outcome, but the for loop and conditional statements are clearly not working, and they also call the function on top of the amount of time I call it in the num argument.

Here is game():

function game(num) {
  var num = prompt('How many rounds?');
  // need to add a way to tell if the input is a number
  var computerScore = 0;
  var playerScore = 0;
  var outcome = playRound();
  for (var i = 0; i < num; i++) {
    if (outcome === 1) {playerScore = playerScore + 1}
    if (outcome === -1) {computerScore = computerScore + 1}


I’ve added my replit here.

Just to follow up here, I’ve got the game and results counter working :slight_smile:, however a couple of things aren’t fully working as hoped:

(1) If the player enters a value that is not valid e.g. not rock paper or scissors, then it does not ignore that in the scoring. I tried to do this in playRound() but it does not seem to be being called within game(). I think this is due to me calling score().

(2) I removed the prompt that allows the player to name the number of games. This seemed like the next step once I’ve finished the original brief.

Things I would like to do:

  • Fix returning the incorrect value
  • Allow player to choose number of games
  • Allow a reply Y to restart

Hey samjdooley,

Welcome to TOP!

Here’s what I see:

  • Right now, you’re checking for invalid user choice after you compare playerSelection and computerSelection. Try moving your check before you compare playerSelection and computerSelection.
    -To let the user choose the number of rounds, try to think about how you could prompt the user to input a number. Then, think about how you could use the number inputted by the user in your for loop to play the desired number of rounds.
    -To play another game, think about how you could prompt the user whether she/he wants to play again. Think about where you could put this prompt in your code. Then, based on the player’s response, you could have your program call game() again or do nothing.

Good luck!