Array doesn't count as array? [Challege: Use Recursion to Create a Range of Numbers]

Tell us what’s happening: I’m getting an error that my result is not an array. Using online javascript interpreters and console.log I’m seeing the correct arrays as output, but freecodecamp isn’t accepting my code for some reason because “Your function should return an array.”.


function rangeOfNumbers(startNum, endNum) {
  if (startNum == endNum) {
    return [endNum];
  } else if (startNum < endNum) {
    result = rangeOfNumbers (startNum, endNum - 1);
    result.push(endNum);
    return result;
  }
}

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

1 Like

Hey there,

can you explain your algorithm step-by-step with a small example, e.g. rangeOfNumber(1,3)?

You have not declared ‘result’

1 Like

You’re right, is that result variable no being declared. once you use the ‘var’ in front of result, it works like a charm!

Strange how freecodecamp accepts a local variable but not a global one :thinking:

It’s really best to avoid global variables.

Yeah, absolutely. I was speeding through the module and was being sloppy with my code.

strict mode doesn’t allow that, and it is active in all the curriculum

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

1 Like

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