Zakres JavaScript
[...Array(5).keys()];
=> [0, 1, 2, 3, 4]
arvinreydelapena
[...Array(5).keys()];
=> [0, 1, 2, 3, 4]
[...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;
}
for (i of range(1, 5)) {
console.log(i);
}
/* Output
* 1 2 3 4 5 */
[...range(1, 5)] // [1, 2, 3, 4, 5]
function range(start, end) {
if(start === end) return [start];
return [start, ...range(start + 1, end)];
}
// 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)))
}