Problem:
- For-loop inside of a function is returning an inexplicable result (1st picture, highlighted in yellow on line 150)
- Same for-loop copy-pasted outside of the function returns the expected result (2nd pic, highlighted in purple on line 164)
- I am not looking for feedback on the approach to the algo problem - only trying to understand the above
Context:
- rock-paper-scissors algo problem, to return an array of nested arrays representing the permutations of hands possible, based on number of rounds played passed in as argument
- function is recursive, with 'n' rounds to play as argument, [[]] as default parameter
- lines 136 - 142 : because total permutations possible is 3 ^ n, it takes the default parameter and duplicates each element 3 times (newOutput)
- lines 146 - 149 : for-loop through newOutput, adding 'rock' to first, 'paper' to second, 'scissors' to third nested array, so on
Question
The duplication of elements in line 136 is impacting the result of for-loop at 146, but why? It's inexplicable since the thread of execution has already moved onto line 146.
code below:
function rockPaperScissors(num, output = [[]]) {
  // when num is 0, return output
  if (num === 0) return output;
  const moves = ['rock', 'paper', 'scissors']
  // take output parameter, and duplicate each of the existing nested array 3 times 
  const newOutput = output.reduce((acc, curr) => {
    let i = 1;
    while (i <= 3) {
      acc.push(curr);
      i++
    }
    return acc;
  }, [])
  // iterate through newOutput, push to each nested array rock first, then paper, then scissors, so on 
  for (let i = 0; i < newOutput.length; i++) {
    newOutput[i].push(moves[i%3]);
  }
  return rockPaperScissors(num - 1, newOutput);
}
rockPaperScissors(1)
// should return [[rock], [paper], [scissors]]
// instead, returns [[rock, paper, scissors], [rock, paper, scissors], [rock, paper, scissors]]

newOutput[i].push(…), the code you posted as text doesn't.movesarray, how are the moves supposed to get into the result?