Getting the exact output but code is not being accepted

Tell us what’s happening:
Describe your issue in detail here.
i am getting the exact output in console . then what’s wrong with my code . please help me.

  **Your code so far**

let arr = []
function rangeOfNumbers(startNum, endNum) {
if(startNum === endNum){
    return arr.push(endNum)
}else{
  arr.push(startNum)
  rangeOfNumbers(startNum + 1,endNum)
}
 return arr
};
console.log(rangeOfNumbers(1, 5))

  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

You need to use the return values of the recursive function calls. Using a global variable to create a faux-recursion is fragile, as described above.

1 Like

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