DEV Community

chandra penugonda
chandra penugonda

Posted on

Get the integers within a range (x, y) using recursion

Problem Statement

Get the integers within a range (x, y) 

input: (2,9)
output: [3,4,5,6,7,8] 

var range = function(x, y) {
   // start here
};

Enter fullscreen mode Exit fullscreen mode

Solution

var range = function (x, y) {
  if (y === x + 1) return [x];
  return [x].concat(range(x + 1, y));
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)