- function baseRange(start, end, step=1, fromRight=false) {
- let index = -1,
- length = Math.max(Math.ceil((end - start) / (step)), 0),
- result = Array(length);
- while (length--) {
- result[fromRight ? length : ++index] = start;
- start += step;
- }
- return result;
- }
- let t = baseRange(0,9,4,false);
- const baseRange = function (...arg) {
- let [start, end, step = 1, fromRight = false] = arg;
- let index = -1,
- length = Math.max(Math.ceil((end - start) / (step)), 0),
- result = Array(length);
- while (length--) {
- result[fromRight ? length : ++index] = start;
- start += step;
- }
- return result;
- }
- let t = baseRange(0, 9, 4, false);
code
Test A fond la forme
range(" 0b011 "," 5",0.5));Range avec test !
Notion avancée
Notez également l'utilisation d'un générateur
let range = function(start,end,step){
return {
*[Symbol.iterator]() {
let value = start;
while (value < end) {
yield value;
value = value + step;
}
}
}
}
for (let e of range(1,6,2)) console.log(e);
let [a,b,...c] = [...range(1,6,1)]
console.log(a,b,c)