This looks to pass the requirements. I am sure someone can come up with a more eloquent answer. But the tricky parts are related to mostly the step parameter (negative and 0) and backwards movement (end < start) and the fact that it tries to do the "right" thing (stepping from 0 to -3 should not be possible witha step of 1, but it seems to automatically flip the step to be negative [again from reading the tests]).

function range (start, end, step = 1) {
  if (end === undefined && start !== undefined) {
    end = start;
    start = 0;
  }

  // step should be negative if end is less than start
  if (end < start && step > 0) {
     step = step * -1
  }


  // if the step is 0 you will never allocate the correct array length
  const stepSize = (step == 0) ? 1 : step
  const size = Math.abs(Math.ceil((end - start) / stepSize))


  // need to exit if there is nothing to return
  if (size == Infinity) {
    return []
  }

  // step has some really odd behavior (from tests)
  //     specifically a step of 0, which seems to repeat the start?
  const incrementStep = (x, y) =>
        (step == 0) ? start : x + y * step

  return Array(size).
    fill(start).
    map(incrementStep)
}

console.log(range(4)); // => [0, 1, 2, 3]
console.log(range(-4)); // => [0, -1, -2, -3]
console.log(range(1, 5)); // => [1, 2, 3, 4]
console.log(range(0, 20, 5)); // => [0, 5, 10, 15]
console.log(range(0, -4, -1)); // => [0, -1, -2, -3]
console.log(range(1, 4, 0)); // => [1, 1, 1]
console.log(range(0)); // => []
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from akaphenom on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › lodash-_-range-method
Lodash _.range() Method - GeeksforGeeks
October 20, 2023 - Lodash _.range() method is used to create an array of numbers progressing from the given start value up to, but not including the end value. A step of -1 is used if a negative value of start is specified without an end or step.
🌐
Lodash
lodash.info › doc › range
range - Lodash documentation
_.range(4); // => [0, 1, 2, 3] _.range(-4); // => [0, -1, -2, -3] _.range(1, 5); // => [1, 2, 3, 4] _.range(0, 20, 5); // => [0, 5, 10, 15] _.range(0, -4, -1); // => [0, -1, -2, -3] _.range(1, 4, 0); // => [1, 1, 1] _.range(0); // => []
🌐
Lodash
lodash.com › docs
Lodash Documentation
Many lodash methods are guarded to work as iteratees for methods like _.every, _.filter, _.map, _.mapValues, _.reject, and _.some. The guarded methods are: ary, chunk, curry, curryRight, drop, dropRight, every, fill, invert, parseInt, random, range, rangeRight, repeat, sampleSize, slice, some, sortBy, split, take, takeRight, template, trim, trimEnd, trimStart, and words
🌐
npm
npmjs.com › package › lodash.range
lodash.range - npm
August 13, 2016 - The lodash method _.range exported as a Node.js module.
      » npm install lodash.range
    
Published: Aug 13, 2016
Version: 3.2.0
🌐
Dustin John Pfister
dustinpfister.github.io › 2018 › 10 › 02 › lodash_range
Creating an array of numbers with _.range in lodash | Dustin John Pfister at github pages
October 20, 2021 - Sometimes when working on a javaScript project there is a need to create a range of numbers in an array, with lodash there is the _.range method than can be used to quickly make a range of numbers. Th
Top answer
1 of 1
1

This looks to pass the requirements. I am sure someone can come up with a more eloquent answer. But the tricky parts are related to mostly the step parameter (negative and 0) and backwards movement (end < start) and the fact that it tries to do the "right" thing (stepping from 0 to -3 should not be possible witha step of 1, but it seems to automatically flip the step to be negative [again from reading the tests]).

function range (start, end, step = 1) {
  if (end === undefined && start !== undefined) {
    end = start;
    start = 0;
  }

  // step should be negative if end is less than start
  if (end < start && step > 0) {
     step = step * -1
  }


  // if the step is 0 you will never allocate the correct array length
  const stepSize = (step == 0) ? 1 : step
  const size = Math.abs(Math.ceil((end - start) / stepSize))


  // need to exit if there is nothing to return
  if (size == Infinity) {
    return []
  }

  // step has some really odd behavior (from tests)
  //     specifically a step of 0, which seems to repeat the start?
  const incrementStep = (x, y) =>
        (step == 0) ? start : x + y * step

  return Array(size).
    fill(start).
    map(incrementStep)
}

console.log(range(4)); // => [0, 1, 2, 3]
console.log(range(-4)); // => [0, -1, -2, -3]
console.log(range(1, 5)); // => [1, 2, 3, 4]
console.log(range(0, 20, 5)); // => [0, 5, 10, 15]
console.log(range(0, -4, -1)); // => [0, -1, -2, -3]
console.log(range(1, 4, 0)); // => [1, 1, 1]
console.log(range(0)); // => []
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
TutorialsPoint
tutorialspoint.com › lodash › lodash_range.htm
Lodash - range method
Lodash - Discussion · Selected ... · _.range([start=0], end, [step=1]) Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end....
🌐
GeeksforGeeks
geeksforgeeks.org › lodash-_-inrange-method
Lodash _.inRange() Method | GeeksforGeeks
May 3, 2025 - Return Value: This method returns true if the number is in the range, else false. Example 1: In this example, we are checking whether the given value is present between the given start and end number, also start number is inclusive. ... // Requiring the lodash library const _ = require("lodash"); // Use of _.inRange method console.log(_.inRange(12, 10)); console.log(_.inRange(10, 12)); console.log(_.inRange(5.6, 5)); console.log(_.inRange(5.6, 6));
Find elsewhere
🌐
Go Make Things
gomakethings.com › how-to-recreate-the-lodash-inrange-method-with-vanilla-js
How to recreate the lodash inRange() method with vanilla JS | Go Make Things
The lodash _.inRange() method takes a number, and checks to see if it’s between two other numbers. You pass in the number, the start of the range, and the end of the range as arguments.
🌐
GitHub
github.com › orgs › types › packages › npm › package › lodash.range
Package lodash.range · GitHub
December 13, 2024 - TypeScript definitions for lodash.range · Learn more about npm packages · $ npm install @types/lodash.range@3.2.6 · "@types/lodash.range": "3.2.6" 3.2.5 · Published over 6 years ago · 0 · 3.2.3 · Published over 6 years ago · 0 · 3.2.1 · Published over 6 years ago ·
🌐
Medium
artemee-lemann.medium.com › range-function-in-javascript-b12fbff42d03
range() function in JavaScript. How many times have to been doing… | by Artemee Lemann | Medium
December 1, 2023 - Just to get an array of some length with filled values? Or you have imported the lodash range funtion?
🌐
npm
npmjs.com › package › @types › lodash.range
@types/lodash.range - npm
// Generated from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/scripts/generate-modules.ts import { range } from "lodash"; export = range;
      » npm install @types/lodash.range
    
Published: Nov 07, 2023
Version: 3.2.9
🌐
Career Karma
careerkarma.com › blog › javascript › creating ranges with javascript
Creating Ranges with JavaScript | Career Karma
December 29, 2020 - If we want to create a range of numbers and we want them as fast as possible we can use the Lodash _.range() util method.
🌐
CodeSandbox
codesandbox.io › examples › package › lodash.range
lodash.range examples - CodeSandbox
Use this online lodash.range playground to view and fork lodash.range example apps and templates on CodeSandbox.
🌐
Reddit
reddit.com › r/javascript › using lodash _.filter to find a range of values
r/javascript on Reddit: Using lodash _.filter to find a range of values
May 4, 2016 -

I am trying to use the lodash _.filter function to filter a range of values between for example 43 and 47. I have gotten it to filter out everything that is lower then 43 but I don’t know how to filter it for numbers above 47.

I know this is probably relatively simple, but I cant find by googling how to write it correctly.

var charactersStar = [
    { 'user': 'trump',  'rank': 36, 'active': true },
    { 'user': 'fred',    'rank': 30, 'active': false },
    { 'user': 'luke',  'rank': 47, 'active': true },
    { 'user': 'hillary',    'rank': 44, 'active': false },
    { 'user': 'bernie',  'rank': 36, 'active': true },
    { 'user': 'john',    'rank': 40, 'active': false },
    { 'user': 'pebbles', 'rank': 1,  'active': true }
];

var result = _.filter(charactersStar, function(o) {
    return o.rank > 42;
});

console.log(result);
🌐
Lodash
lodash.info › doc › rangeRight
rangeRight - Lodash documentation
_.rangeRight(4); // => [3, 2, 1, 0] _.rangeRight(-4); // => [-3, -2, -1, 0] _.rangeRight(1, 5); // => [4, 3, 2, 1] _.rangeRight(0, 20, 5); // => [15, 10, 5, 0] _.rangeRight(0, -4, -1); // => [-3, -2, -1, 0] _.rangeRight(1, 4, 0); // => [1, 1, 1] _.rangeRight(0); // => []
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › lodash-_-rangeright-method
Lodash _.rangeRight() Method - GeeksforGeeks
November 13, 2023 - Lodash _.rangeRight() method is used to create an array of numbers progressing from the given start value to the given, but not including, the given end value. It populates the values in descending order.