const Range2 = function* (to = 0, step = 1, from = 0) { let i = 0, length = Math.floor((to - from) / step) + 1; while (i < length) yield from + i++ * step; }; [...Range2(5, -2, 10)]; // From 10 to 5 with step -2 //=> [10, 8, 6] let even4to10 = Range2(10, 2, 4); even4to10.next().value; //=> 4 even4to10.next().value; //=> 6 even4to10.next().value; //=> 8 even4to10.next().value; //=> 10 even4to10.next().value; //=> undefined
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