Javascript Array methods to line EVEN numbers

Hi guys, I’ve been sitting with this problem for a day. What are other alternatives besides FOR loop to fill an array with EVEN numbers in a range between 2 to n.

It’s this just for an exercise, or is there a specific problem you are trying to solve and a for loop is not appropriate?

If you just want another type of loop, you could look into WHILE loops.

Hi, it is an exercise. Wanted to see if there are any options outside of loops.

You could try recursion - it’s not a great use for it, but as an exercise in avoiding loops it works!

let count = 2;

function fillArray(array, range) {
  array.push(count);
  if (count < range) {
    count += 2
    fillArray(array, range);
  } else {
    console.log(array);
  }
}

fillArray([],10)
1 Like