My code work correct. But i can't pass a test!

Hi everybody. I just write a code and he work correctly like was indicated in task. But i can’t pass a test.
Yeah maybe i did something wrong?


var countArray=[];
function rangeOfNumbers(startNum, endNum) {

 if(  startNum > endNum)
 {
   return [];
 }
 else if (startNum===endNum)
 {
   
  countArray.unshift(endNum); 
  return countArray;
 }
 else{ 
    
  countArray.unshift(endNum);
 }
 return rangeOfNumbers(startNum,endNum-1);
};

console.log(rangeOfNumbers(1,5));

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

2 Likes

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
3 Likes

@ieahleen Thank you!