typestar

Range array in TypeScript

Builds an array of numbers, the eager counterpart to a generator.

function range(start: number, stop?: number, step = 1): number[] {
  if (stop === undefined) {
    stop = start;
    start = 0;
  }
  const out: number[] = [];
  for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
    out.push(i);
  }
  return out;
}

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
239
Tokens
82
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 5 of 5 in Functions & tuples, step 20 of 23 in Types & annotations.

← Previous Next →