Skip to curriculum
Jump to the editor

4.3. Fill, range and range from the right

Easy
Topics

This question is in

4. Array utilities
15–20 min

Pagination controls need the page numbers 1 to 10. A calendar needs the hours 0 to 23, and a countdown needs 10 down to 1. A grid editor resets a row of cells to empty. These are small helpers that every codebase ends up writing: make a list of numbers, or overwrite part of an array with one value.

Write three functions in range.js: fill, which writes one value into a part of an existing array; range, which makes a list of numbers; and rangeRight, which makes the same list in reverse.

Signature

JAVASCRIPT
function fill(array, value, start, end) // returns array itself
function range(start, end, step)        // returns a new array of numbers
function rangeRight(start, end, step)   // returns a new array of numbers
  • fill changes array in place: indexes start up to, but not including, end get value. start defaults to 0, end to array.length.
  • range(end) counts from 0. range(start, end) counts from start. Neither includes end.
  • step is the distance between numbers. Without it, the step is 1, or -1 when end is below start.
  • rangeRight returns what range returns with the same arguments, last number first.

Requirements

fill:

  1. It fills from start up to, not including, end, and returns the same array.
  2. A negative start or end counts from the end of the array (-1 is the last index).
  3. Positions before the start or after the end of the array are clipped to it; when start is not below end, nothing changes.
  4. Fractional positions are cut toward zero (1.7 is 1, -1.5 is -1).
  5. It fills holes, and every filled slot gets the same value (the same object, not copies).
  6. It does not use Array.prototype.fill.

range and rangeRight:

  1. range(4) is [0, 1, 2, 3]; range(1, 5) is [1, 2, 3, 4]; range(0, 20, 5) is [0, 5, 10, 15].
  2. Without a step, a range whose end is below its start counts down: range(-4) is [0, -1, -2, -3].
  3. A step that points away from end, a step of 0, or start equal to end gives [].
  4. Fractional steps give the right count and never include end, however small the step: range(0, 1, 0.1) has 10 numbers, never 11, and range(0.1, 0.4, 0.1) has 3 (the last one is about 0.3).
  5. rangeRight gives the same numbers as range, in reverse.
  6. Both handle 100,000 numbers quickly, and any finite start, end and step, even endpoints further apart than Number.MAX_VALUE (range(-Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE) is [-Number.MAX_VALUE, 0]). A range with more numbers than an array can hold (2 ** 32 - 1) throws a RangeError instead of looping forever.

Examples

JAVASCRIPT
fill([1, 2, 3, 4], 0, 1, 3);   // [1, 0, 0, 4]
fill([1, 2, 3, 4], "x", -2);   // [1, 2, "x", "x"]
fill([1, 2, 3], 9, 5);         // [1, 2, 3]: nothing to fill
JAVASCRIPT
range(4);            // [0, 1, 2, 3]
range(-4);           // [0, -1, -2, -3]
range(0, 20, 5);     // [0, 5, 10, 15]
range(1, 4, 0);      // []
JAVASCRIPT
rangeRight(4);           // [3, 2, 1, 0]
rangeRight(0, 1, 0.25);  // [0.75, 0.5, 0.25, 0]

Environment

  • Plain JavaScript in a browser page; the tests call the globals fill, range and rangeRight from range.js.
  • The tests pass numbers only (no strings or NaN for start, end or step).
  • Array.prototype.fill is not allowed; the tests remove it while fill runs.

Hints

  1. For fill, turn each position into a real index between 0 and the length first. Then the loop is simple.
  2. For range, can you work out how many numbers there will be before you make any?
  3. What happens to 0.1 + 0.1 + 0.1 in JavaScript?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…