### Chryen github logo Jan 19Updated on Jan 25, 2020・3 min read

Hello Mr. Reader, welcome to the world of coding. I, Chris Nguyen, am here to give you a quick and easy explanation on how to solve that Rock, Paper, Scissors toy problem!

Your toy problem may look a little something like this:

Write a function rockPaperScissors that takes in a number rounds and generates every sequence of throws a singleplayer could have over game of rock-paper-scissors with the provided amount of rounds.

``````const rockPaperScissors = (rounds) => {

};
``````

Let's start by listing our IOCE:

• Inputs: A number
• Outputs: An array of arrays. The subarrays contain the strings 'rock' 'paper' and 'scissors'
• Constraints: None
• Edge Cases: None

Now, let's try to get a better GRASP on the problem. We know that there are only 3 possible choices to pick from in a game of rock, paper, scissors. And we must take into consideration the number of rounds there will be. Going off that piece of information and following our IOCE, we can add the following to our code:

``````const rockPaperScissors = (rounds) => {
let outcomes = [];
let plays = ['rock', 'paper', 'scissors'];

return outcomes;
};
``````

You might be thinking, "Maybe we can solve this using a for-loop inside a for-loop and so on..." While that thought may work in this case, it may be a bit taxing to write out a bunch of nested for-loops. We can actually solve this using our good friend recursion. For those who do not not have any knowledge on this, recursion is essentially a for-loop for using functions. I'll explain what goes into a recursive function.

What exactly goes into our recursion? First, we need to set up a base case; this base case will signify when the entire function should stop. And lastly, we need our recursive method that will reuse the parent function, essentially creating a loop. Here is an example:

``````// helper function to get all the outcomes
let getOutcomes = function(played, roundsLeft) {
// base case
if (roundsLeft === 0) {
// add played to outcomes array
outcomes.push(played);
} else {
// loop through plays
for (let i = 0; i < plays.length; i++){
// recursive method
getOutcomes(played.concat(plays[i]), roundsLeft - 1);
}
}
};
``````

Finally, we call our helper function passing in an empty array and rounds. Our final code should look like this:

``````const rockPaperScissors = (rounds) => {
// Input: number
// Output: Array
// Constraints: None
// Edge Cases: None

// array to hold each play
let outcomes = [];
// 3 choices
let plays = ['rock', 'paper', 'scissors'];
// helper function to get all the outcomes
let getOutcomes = function(played, roundsLeft) {
// base case
if (roundsLeft === 0) {
// add played to outcomes array
outcomes.push(played);
} else {
// loop through plays
for (let i = 0; i < plays.length; i++){
// recursive method
getOutcomes(played.concat(plays[i]), roundsLeft - 1);
}
}
};
// call helper function passing in an empty array and rounds
getOutcomes([], rounds);
// return the results
return outcomes;
};
``````

Feel free to copy and paste this code to clarify if your toy problem passes. Your result should look like this:

``````[ [ 'rock', 'rock', 'rock' ],
[ 'rock', 'rock', 'paper' ],
[ 'rock', 'rock', 'scissors' ],
[ 'rock', 'paper', 'rock' ],
[ 'rock', 'paper', 'paper' ],
[ 'rock', 'paper', 'scissors' ],
[ 'rock', 'scissors', 'rock' ],
[ 'rock', 'scissors', 'paper' ],
[ 'rock', 'scissors', 'scissors' ],
[ 'paper', 'rock', 'rock' ],
[ 'paper', 'rock', 'paper' ],
[ 'paper', 'rock', 'scissors' ],
[ 'paper', 'paper', 'rock' ],
[ 'paper', 'paper', 'paper' ],
[ 'paper', 'paper', 'scissors' ],
[ 'paper', 'scissors', 'rock' ],
[ 'paper', 'scissors', 'paper' ],
[ 'paper', 'scissors', 'scissors' ],
[ 'scissors', 'rock', 'rock' ],
[ 'scissors', 'rock', 'paper' ],
[ 'scissors', 'rock', 'scissors' ],
[ 'scissors', 'paper', 'rock' ],
[ 'scissors', 'paper', 'paper' ],
[ 'scissors', 'paper', 'scissors' ],
[ 'scissors', 'scissors', 'rock' ],
[ 'scissors', 'scissors', 'paper' ],
[ 'scissors', 'scissors', 'scissors' ] ]
``````

In conclusion, Rock, Paper, Scissors was a fun toy problem. It reinforces some nice, basic fundamentals taught early on in JavaScript. And I hope you have gotten something out of this blog.

DISCUSS (2) Classic DEV Post from Nov 1 '19

## How I Got Hired at DEV (and Every Other Tech Job)   