Modified Mozilla Sequence Generator

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)

by MegaScience

JavaScript

// Overcomplicated Sequence Generator via:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)

const arr = []

// Sequence generator function (commonly referred to as "range", e.g. Clojure, PHP etc)
const range = (start, stop, step, mapFn) => Array.from({ length: (stop - start) / step + 1}, (_, i) => mapFn ? mapFn(start + (i * step)) : start + (i * step))

// Generate numbers range 0..4
arr.push(range(0, 4, 1))
// [0, 1, 2, 3, 4] 

// Generate numbers range 1..10 with step of 2 
arr.push(range(1, 10, 2)) 
// [1, 3, 5, 7, 9]

// Generate the alphabet using Array.from making use of it being ordered as a sequence
arr.push(range('A'.charCodeAt(0), 'Z'.charCodeAt(0), 1, String.fromCharCode))
// ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

document.body.innerHTML = arr.map(JSON.stringify).join('<br/>')