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

chandrapenugonda

chandra penugonda

Posted on February 14, 2022

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
💖 💪 🙅 🚩
chandrapenugonda
chandra penugonda

Posted on February 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related