1function range(start, end) {
2 /* generate a range : [start, start+1, ..., end-1, end] */
3 var len = end - start + 1;
4 var a = new Array(len);
5 for (let i = 0; i < len; i++) a[i] = start + i;
6 return a;
7}
1for (i of range(1, 5)) {
2 console.log(i);
3}
4/* Output
5 * 1 2 3 4 5 */
6
7[...range(1, 5)] // [1, 2, 3, 4, 5]
8
1
2function range(start, end, interval = 0) {
3 let arr = [];
4 interval = interval > 0 ? interval-1 : 0
5 for (let i = start; i < end; i++) {
6 arr.push(i)
7 i+=interval;
8 }
9 return arr
10 }
11// example range(0 , 100 , 5)
1// while ES6 does not have any similar methods built-in
2// the same basic functionality can be implemented by:
3[...Array(5).keys()];
4 => [0, 1, 2, 3, 4]
5
6String.fromCharCode(...[...Array('D'.charCodeAt(0) - 'A'.charCodeAt(0) + 1).keys()].map(i => i + 'A'.charCodeAt(0)));
7 => "ABCD"
8
9// OR //
10
11function range(size, startAt = 0) {
12 return [...Array(size).keys()].map(i => i + startAt);
13}
14
15function characterRange(startChar, endChar) {
16 return String.fromCharCode(...range(endChar.charCodeAt(0) -
17 startChar.charCodeAt(0), startChar.charCodeAt(0)))
18}