DEV Community

Discussion on: How to create range in Javascript

Collapse
 
bigzude profile image
Big-Zude

After reading through this helpful discussion this what I came up with, I added a condition if the start point of the range is greater than the end point. This my very first contribution

function range(s, e) {
  var arr = [];
  for (let i = s; i <= e; i++) {
      arr.push(i);
  }
  return  s > e ? Array(s - e + 1).fill(0).map((x, y) => - y + s):arr;
}
console.log(range(10,1)
Enter fullscreen mode Exit fullscreen mode