DEV Community

Discussion on: Use Recursion to Create a Range of Numbers

Collapse
 
alfredosalzillo profile image
Alfredo Salzillo
function range(start, end, acc = []) {
   If (start === end) return [...acc, end]
   return range( start + 1, end, [...acc, start])
}
Enter fullscreen mode Exit fullscreen mode

In tail recursion is better. It's optimized by the JavaScript engine and will not fill the heap at any call, and will have the same space complexity of an iterative one.
And as a plus can be a one liner.