class Range { constructor(total = 0, step = 1, from = 0) { this[Symbol.iterator] = function* () { for (let i = 0; i < total; yield from + i++ * step) {} }; } } [...new Range(5)]; // Five Elements //=> [0, 1, 2, 3, 4] [...new Range(5, 2)]; // Five Elements With Step 2 //=> [0, 2, 4, 6, 8] [...new Range(5, -2, 10)]; // Five Elements With Step -2 From 10 //=>[10, 8, 6, 4, 2] [...new Range(5, -2, -10)]; // Five Elements With Step -2 From -10 //=> [-10, -12, -14, -16, -18] // Also works with for..of loop for (i of new Range(5, -2, 10)) console.log(i); // 10 8 6 4 2
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter