function el(arr) {
let i = 0;
return function gen() {
if (i < arr.length) {
let val = arr[i];
i += 1;
return val;
}
}
}
const myArray = [1, 2, 3];
const myGenerator = el(myArray);
console.log(myGenerator()); // 1
console.log(myGenerator()); // 2
console.log(myGenerator()); // 3
console.log(myGenerator()); // undefined
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter