Created
July 22, 2017 21:18
-
-
Save cmehrabian/9e9f4d2234c49ea4b6563091490c5058 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Write a function that generates every sequence of throws a single | |
* player could throw over a three-round game of rock-paper-scissors. | |
* | |
* Your output should look something like: | |
* [["rock", "rock", "rock"], | |
* ["rock", "rock", "paper"], | |
* ["rock", "rock", "scissors"], | |
* ["rock", "paper", "rock"], | |
...etc... | |
] | |
* | |
* Extra credit: | |
* - Make your function return answers for any number of rounds. | |
* Example: | |
* rockPaperScissors(5); // => [['rock', 'rock', 'rock', 'rock', 'rock'], etc...] | |
* | |
*/ | |
var rockPaperScissors = function (rounds) { | |
// TODO: Function that generates every sequence of throws given 3 rounds | |
var plays = ['rock', 'paper', 'scissors']; | |
// store all plays arr of arrs | |
var outcomes = []; | |
// recursive function | |
var getOutcomes = (roundsLeft, playsSoFar) => { | |
// base case | |
if (roundsLeft === 0) { | |
outcomes.push(playsSoFar); | |
} else { | |
// for over each play | |
for(var i = 0; i < plays.length; i++) { | |
getOutcomes(roundsLeft - 1, playsSoFar.concat(plays[i])); | |
} | |
} | |
} | |
getOutcomes(rounds, []); | |
return outcomes; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment