[...Array(5).keys()];
=> [0, 1, 2, 3, 4]
function range(start, end) {
/* generate a range : [start, start+1, ..., end-1, end] */
var len = end - start + 1;
var a = new Array(len);
for (let i = 0; i < len; i++) a[i] = start + i;
return a;
}
// while ES6 does not have any similar methods built-in
// the same basic functionality can be implemented by:
[...Array(5).keys()];
=> [0, 1, 2, 3, 4]
String.fromCharCode(...[...Array('D'.charCodeAt(0) - 'A'.charCodeAt(0) + 1).keys()].map(i => i + 'A'.charCodeAt(0)));
=> "ABCD"
// OR //
function range(size, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
}
function characterRange(startChar, endChar) {
return String.fromCharCode(...range(endChar.charCodeAt(0) -
startChar.charCodeAt(0), startChar.charCodeAt(0)))
}
function range(start, end, interval = 0) {
let arr = [];
interval = interval > 0 ? interval-1 : 0
for (let i = start; i < end; i++) {
arr.push(i)
i+=interval;
}
return arr
}
// example range(0 , 100 , 5)
[...Array(5).keys()] // [0, 1, 2, 3, 4]
function range(start, stop, step) {
var a = [start], b = start;
while (b < stop) {
a.push(b += step || 1);
}
return a;
}
if (x >= 0.001 && x <= 0.009) {
// something
}
function between(x, min, max) {
return x >= min && x <= max;
}
// ...
if (between(x, 0.001, 0.009)) {
// something
}