Generate evenly spaced number sequences with start, end, step
Chart axes, pagination controls, and time pickers all need lists of evenly spaced numbers: page numbers 1 to 10, ticks every 0.25, a countdown from 10 to 1.
Implement sequence(start, end, step). It returns an array of numbers that begins at start, moves by step, and stops before reaching end.
sequence(end) // same as sequence(0, end)
sequence(start, end)
sequence(start, end, step)
end and start is 0. Passing undefined for end or step is the same as leaving it out.end is exclusive: it is never included.step is omitted, it is 1 if start <= end and -1 otherwise, so sequence(5, 1) counts down.i is exactly start + i * step. Do not add step repeatedly: that accumulates floating-point error.< end. For a negative step, keep them while they are > end.end (for example sequence(0, 5, -1)), or start === end, return [].RangeError if step is 0, or if any argument that is given is not a finite number (NaN, Infinity, a string, and so on).sequence(4); // => [0, 1, 2, 3]
sequence(1, 6); // => [1, 2, 3, 4, 5]
sequence(5, 1); // => [5, 4, 3, 2]
sequence(0, 20, 5); // => [0, 5, 10, 15]
sequence(10, 0, -3); // => [10, 7, 4, 1]
sequence(0, 1, 0.25); // => [0, 0.25, 0.5, 0.75]
sequence(0, 5, -1); // => []
sequence(3, 3); // => []
sequence(0, 5, 0); // throws RangeError
sequence(0, Infinity); // throws RangeError
start + i * step?// Adding 0.1 ten times gives 0.9999999999999999, which is still < 1,
// so a loop that adds step would return 11 elements for sequence(0, 1, 0.1).
// Using start + i * step, element 10 is exactly 1, so the result has 10 elements.