Hello everyone!
Today we're going to working on a toy problem called rockPaperScissors, This toy problem was a lot of fun to work on! The objective of this problem is to generate as many combinations possible by the number of rounds is given. So if the user wanted to input of 2 rounds, this function should give all possible outcome of rock paper scissors
const rockPaperScissors = (rounds) => {
}
To start lets try to break the problem down a little bit
const rockPaperScissors = (rounds) => {
// This function wants a all possible outcome of rock paper scissors depending on the rounds that is being inputed(number)
// Input: Number of Rounds
// Output: possible outcome stored in an array
// Lets create an outcome array to be receiving the possible outcome and return as final result
let outcome = [];
// create an array storage containing respective rock, paper, scissors to be iterated through.
let rps = ['rock', 'paper', 'scissors'];
};
With the first thought, couldn't we just use a for-loop and keep pushing the results into the array to solve the problem? Well it is possible to use a for-loop way, but it'll become tedious and redundant dealing with nested for-loops, but my recommendation would be to use a recursive helper function within the function and calling it in the function.
Alright we're going to create a recursive function within our function that will go access our rock paper scissors array and add them into our output.
const roundChoices = function(round, roundNumber) {
//iterate through the rock paper scissors array to give possible options
rps.forEach((options) => {
// put those options in our round array
round.push(options);
// using Ternary to check if the roundNumber is equal to the rounds being inputed.
roundNumber === rounds ?
// if so pushed the store the round results into or outcome
// if not recursively call function with the roundNumber plue 1
outcome.push(round.slice()) : roundChoices(round, roundNumber + 1);
// pop off the round result;
round.pop();
});
};
In the code above we're going to iterate through our rock paper scissors array and put the options into our array param and check to see if the roundNumber param is equal to the initial rounds param of our given function. If so we would complete the function by adding all of the results into our outcome array. If not it'll recall it's self with the round Number incremented
Our final solution should be:
const rockPaperScissors = (rounds) => {
// This function wants a all possible outcome of rock paper scissors depending on the rounds that is being inputed(number)
// Input: Number of Rounds
// Output: possible outcome stored in an array
// Lets create an outcome array to be receiving the possible outcome and return as final result
let outcome = [];
// create an array storage containing respective rock, paper, scissors to be iterated through.
let rps = ['rock', 'paper', 'scissors'];
// create a recursive helper function that will take the
const roundChoices = function(round, roundNumber) {
//iterate through the rock paper scissors array to give possible options
rps.forEach((options) => {
// put those options in our round array
round.push(options);
// using Ternary to check if the roundNumber is equal to the rounds being inputed.
roundNumber === rounds ?
// if so pushed the store the round results into or outcome
// if not recursively call function with the roundNumber plus 1
outcome.push(round.slice()) : roundChoices(round, roundNumber + 1);
// pop off the round result;
round.pop();
});
};
// call helper function
roundChoices([], 1);
// return the outcome
return(outcome);
};
Thank you for taking the time to viewing over this blog.
A good little take away is making a little recursive helper function to do all the work for you instead of having to brute-force nested loops!
Top comments (0)