Basic JavaScript - Use Recursion to Create a Range of Numbers

Hello. I am trying to practice recursion by removing all even numbers in a sequence.
My console reads that I am ‘exceeding my stack size’. What is wrong with the code?

Thanks in advance :slight_smile:

const myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function remEven(arr) {
let i = 0;
if (i === arr.length) {
} else if (arr[i] % 2 === 0) {
  remEven(arr, i + 1);
  return arr;
} else {
  remEven(arr, i + 1);
  return arr;
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers

Link to the challenge:

You are explicitly saying that i will always be 0 and never be arr.length unless arr is empty

You are calling your function with two arguments, but there is only one in the function signature. Also, you are ignoring the return value

Also, trying to index into an array as you remove elements will lead to indexing issues

Wow. So I’ve basically made a monster :sweat_smile: :sweat_smile:

I really appreciate you taking the time. I’m sort of throwing myself at recursion to see if I can wrap my head around it without knowing too much, but I’m going to do a full day on it today.

I’ll come back to look at this. Cheers.

Recursion does tend to be messy.

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