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)