Javascript Array from() create range function

Description

Javascript Array from() create range function

const range = (start, stop, step) => 
          Array.from({ length: (stop - start) / step + 1}, 
                      (_, i) => start + (i * step)
                    );//from w  w  w.  jav  a  2 s .  c  o  m

// Generate numbers range 0..4
let a = range(0, 4, 1);
console.log(a);// [0, 1, 2, 3, 4] 

// Generate numbers range 1..10 with step of 2 
a = range(1, 10, 2); 
console.log(a);// [1, 3, 5, 7, 9]

// Generate the alphabet 
a = range('A'.charCodeAt(0), 'Z'.charCodeAt(0), 1).map(x => String.fromCharCode(x));
console.log(a);



PreviousNext

Related