const loopOverArray = (arr) => {
let index = -1;
const getNextElementFromArray = () => {
index++;
const nextIndex = index % arr.length;
return arr[nextIndex];
};
return getNextElementFromArray;
};
const getNextElement = loopOverArray([1, 2, 3]);
console.log(getNextElement()); // 1
console.log(getNextElement()); // 2
console.log(getNextElement()); // 3
console.log(getNextElement()); // 1
Thanks for reading π
Follow @codedrops.tech for more.
Instagram β Twitter β Facebook
Micro-Learning β Web Development β Javascript β MERN stack
codedrops.tech
Projects
File Ops - A VS Code extension to easily tag/alias files & quick switch between files
Top comments (0)