Created
October 2, 2020 19:27
-
-
Save kzlsakal/21aa4543ece70a21c990e5c944e27ce7 to your computer and use it in GitHub Desktop.
Rock Paper Scissors JavaScript Permutation Solution (pre-ES6)
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
var rockPaperScissors = function (n) { | |
// Capture the n or give it a default value of 3 | |
n = n || 3; | |
// Store the possible moves | |
const moves = ['R', 'P', 'S']; | |
// Store the possible game outcomes | |
const possibilities = []; | |
// Iterate on the number of options by the power of number of rounds | |
for (let i = 0; i < moves.length ** n; i++) { | |
// Store the current game outcome of all rounds | |
let game = ''; | |
// Decide the indexes of each round 'base'd on the number of options | |
let indexOnBase = i.toString(moves.length); | |
// Add a '0' index for the indexes that we have not reached yet | |
while (indexOnBase.length < n) { | |
indexOnBase = '0' + indexOnBase; | |
} | |
// Iterate each round | |
for (let j = 0; j < n; j++) { | |
// Add the current round to the game based on its decided index | |
game += moves[indexOnBase[j]]; | |
} | |
// Add the current game outcome to the outcomes list | |
possibilities.push(game); | |
} | |
// Return the possible outcomes | |
return possibilities; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment