var rockPaperScissors = function(rounds) {
var outcomes = [];
var plays = ['rock', 'paper', 'scissors'];
var playedSoFar = [];
var combos = function(roundsToGo) {
// base case
if (roundsToGo === 0) {
outcomes.push(playedSoFar.slice());
return;
}
for (var i = 0; i < plays.length; i++) {
playedSoFar.push(plays[i]);
combos(roundsToGo - 1);
playedSoFar.pop();
}
};
combos(rounds);
return outcomes;
};
console.log(rockPaperScissors(2));
If I take out the slice()
from the playedSoFar
, outcomes
just gets returned as nine empty arrays, instead of all the combinations from a 2 round rock paper scissors game:
[ [ 'rock', 'rock' ],
[ 'rock', 'paper' ],
[ 'rock', 'scissors' ],
[ 'paper', 'rock' ],
[ 'paper', 'paper' ],
[ 'paper', 'scissors' ],
[ 'scissors', 'rock' ],
[ 'scissors', 'paper' ],
[ 'scissors', 'scissors' ] ]
Why doesn't this work when I take out the slice()
from the playedSoFar
?