function iterateOver(...args) {
let [from, to, step=1] = [...args];
const range = {
[Symbol.iterator]() {
this.current = from;
return this;
},
next() {
if (this.current <= to) {
let char = this.current;
this.current = String.fromCharCode(this.current.charCodeAt(0) + step);
return {
done: false,
value: char
};
} else {
return {
done: true
};
}
}
}
return range;
}
for (let num of iterateOver("A","W",5)) {
console.log(num);
}
let t = Array.from(iterateOver("A","J"), c => c.toLowerCase());
console.log(t);
fichier source
fichier source
JS Bin on jsbin.com