Why I am not returning an array with all my values?

Tell us what’s happening:
Hello guys I am having a couple doubt about my code, i have been using repl.it and I have noticed that when I console.log(arr) I get [6] [7] [8] [9] instead of [6,7,8,9] and when I return arr I only get [9].

Any help on this would be nice.

Your code so far


function rangeOfNumbers(startNum, endNum) {
if (startNum  <= endNum ){
   var arr = [];
  var  range = (endNum - startNum + 1) + (startNum - 1)  ;
   arr.unshift(range);
   rangeOfNumbers(startNum, endNum - 1)
   return arr // console.log(arr)
}

};
rangeOfNumbers(6, 9)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

this returns an array, you do nothing with that array, that’s the issue you have - use that array!


this is a really complex way to say var range = endNum

1 Like

Cool I will try again.

I just realize now that part thanks, I will keep trying to see how can I fix it this.

Thanks for you help it was easier than what I though, below my solution.

function rangeOfNumbers(startNum, endNum) {
  if ( startNum <= endNum){
    var range = rangeOfNumbers(startNum, endNum - 1);
    range.push(endNum);
    return range

  }else {
    return []
  }
  
};
rangeOfNumbers(6, 9);