How can I get the right output in the array [closed]

I need to make an array of [0, 1, 2] when I have the number 3 in the parameter. What’s wrong?

const array = [0];
const increment = (num) => {
  if (num > 0) {
    increment(num - 1);
    array.push(num);
  }
  return;
};
console.log(array);
increment(3);

Answer

As pointed in the comment your code almost works you just put console.log before calling the function.

const array = [0];
const increment = (num) => {
  if (num > 0) {
    increment(num - 1);
    array.push(num);
  }
  return;
};

increment(3);

console.log(array);

Shortest solution with Array.from:

const makeArray = length => Array.from({ length }, (_, i) => i);

console.log(makeArray(3));