function range(start, end, step = 1) {
const result = [];
if (step === 0) throw new Error('step cannot be 0');
for (let i = start; step > 0 ? i <= end : i >= end; i += step) {
result.push(i);
}
return result;
}
// Usage
console.log(range(1, 5)); // [1, 2, 3, 4, 5]
console.log(range(0, 10, 2)); // [0, 2, 4, 6, 8, 10]
console.log(range(5, 1, -1)); // [5, 4, 3, 2, 1]
Create a free account and build your private vault. Share publicly whenever you want.