Rock, Paper Scissors

Hi, I’m learning Javascript and I’m trying to make a Rock Paper Scissors (playable in console)
I’ve made a function called singleRound that receive user and CPU choice and check who has won the round

The function singleRound works :

But, I tried to use a loop to repeat function calls and tell if the user has won or lost with a function called game and it doesn’t work

Here’s my try : https://codepen.io/sendzixbt/pen/OJmwyzm

When you say “it doesn’t work,” what is it actually doing? How many rounds do you actually set game() up to play? What mechanism, within game, it’s actually looping?

@Wairua, help me out here - we need to loop, but we don’t know how many times! What might be a good “exit condition” and what might be a good looping tool?

@sendzixbt i mention wairua because we were having a very relevant conversation earlier: Chunky monkey problemo

1 Like

Sorry, I forgot to precise the purpose of game()

It is supposed to execute singleRound() 5 times and tell to the user if he has won in the console
But it doesn’t looping 5 times

oh, i think you want something like this:

function game() {
  for(var i=0; i<5; i++) {
    singleRound();
  }
  if(score['userScore'] > score['computerScore']) {
    return 'win';
  } else {
    return 'loss';
  }
}

And what looping mechanism are you using? An if isn’t loop.

Thanks for your help, @snowmonkey, and @DavidMatthewFraser, it works!!!

Here is it :

function game() {
for(var i=0; i<5; i++) {
singleRound();
}
if(score[‘userScore’] > score[‘computerScore’]) {
return You won! ${score['userScore']}-${score['computerScore']};
} else {
return Looser! ${score['userScore']}-${score['computerScore']};
}
}

And that works, but does it meet the requirements? You’re playing five rounds, rather than playing until one player or the other reaches a score of 5.

Different looping mechanisms might yield different results. As @Wairua and i were discussing, a for loop is great for a known, fixed number of iterations (in this case, five rounds). A while loop is useful for some number of iterations unknown in advance, looping until a condition is met.

If, for example, you were looping until player or computer reached a score of 5, you might need to loop at least five times, or at most nine, but you don’t know in advance.

1 Like

It’s great that you found a solution, but consider how to approach this as a teachable moment - would more be learned by guiding the OP to a solution or by giving a solution outright?

1 Like

I do some research about while loop and I also found another solution

let count = 0;
function game(){
do {
count++;
singleRound();
} while (count != 5)
if (score[‘userScore’] > score[‘computerScore’]) {
return You won! ${score['userScore']}-${score['computerScore']}
} else if (score[‘userScore’] == score[‘computerScore’]) {
return You loose! ${score['userScore']}-${score['computerScore']}
} else {
return Tie! ${score['userScore']}-${score['computerScore']}
}
}

It’s better than the previous loop that I’ve written in my codepen
Thanks for your help!

You could also use as your while loop condition

while( score["userScore"] < 5 && score["computerScore"] < 5 )

Using that, if either score reaches 5, the while is no longer true, exiting the loop.

This it’s what i meant by not knowing how many iterations - if I’m lucky, i win in 5 rounds. One of us will win in 9 rounds, one way or another. But there’s no way of knowing how many to win in advance, as each round is random.

2 Likes

Well, I guess it’ll take a lot of practice before I get good at algorithms but thanks again for your help :pray:

1 Like

Hey,
I am trying to understand the source code, correct me if I’m wrong.
He should use the while loop because the game might go on for uncountable times.
And if that is the case then he’ll be looping the game until an exit condition is met (which i believe it’s either the computer or the user has a score of five).

1 Like

this is where i was confused (there was no looping). But thank you for helping us both.

1 Like

@snowmonkey @sendzixbt @Wairua From how the problem was described, the game is played 5 times and the player with the most points wins.

“It is supposed to execute singleRound() 5 times and tell to the user if he has won in the console”

Given that the number of iterations is known, a for loop should be used. However, it might be better game design to make the game a best of five … this is where you would use a while loop like @snowmonkey suggested.

2 Likes

And that was my point. If you go from the spec given, it’s a best of five, making the number of games known. Thus according to spec, a for loop with a known limit makes sense.

However, a first to five disallows tie games, and makes for a little more challenging thing, as the number of rounds is now unknown, though within finite limits. In this case, a while loop makes sense, as it continues until a particular game state is achieved.

In this case, there is a clear solution: follow the spec. But be aware of other ways, learning as you go.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.