JSFiddle - React, Tailwind, and code Playground

by brigand

JavaScript

function range(min, max, step = 1) {
  const items = [];
  while (true) {
    const next = (items.length * step) + min;
    if (next >= max) {
      return items;
    }
    items.push(next);
  }
}

for (const min of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) {
  for (const max of [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]) {
    for (const step of [1, 2, 3, 4, 5, 6, 7, 8, 9, 0.5, 1.5, 2.5, 3.33337]) {
      const r = range(min, max, step);
      const invalid = r.find(n => n < min || n > max);
      if (invalid != null) {
        console.log(`Out of bounds: range(${min}, ${max}, ${step}) contains ${invalid}\nr: ${r.join(',')}`)
      }
    }
  }
}

console.log(range(0, 9, 2))