I am trying to write a function that takes an array and returns a new array with all elements shifted n indices to the left. For example:
rotLeft([1,2,3,4],2)
// should return [3,4,1,2]
I wrote a recursive function that removes the value at index 0 and assigns it to last index using .shift() and .push().
const rotLeft = (array, n) => {
console.log(array, n); // <-- this prints out expected array
if (!n) return array; // <-- but this returns undefined! :(
array.push(array.shift());
rotLeft(array, n - 1);
};
console.log(rotLeft([1, 2, 3, 4, 5, 6, 7], 9));
How come each console.log(array) prints out the expected array but the array is undefined when the function returns?
