Copy machine using recursion

Hi all,

I solved the copy machine problem in JS using a while loop, but afterwards I got curious about if I could have used recursion to do the same thing. Here’s what I wrote but it’s not working (error says Maximum call stack size exceeded):

function copyMachine(arr, num) {
let newArr = […arr];
if (num = 0){
} else {
return newArr.push(copyMachine(arr, num - 1))

console.log(copyMachine([true, false, true], 2));

Please advise!

You can, but note that:

  • = is the assignment operator, you should use an equality operator for value comparison.
  • the push method returns the length of the changed array, not the changed array itself.
  • Also, push probably isn’t the best choice here.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.