Skip to content

Instantly share code, notes, and snippets.

@ironars
Last active January 8, 2020 12:11
Show Gist options
  • Save ironars/5c62024fe67f6fef4371b0c0a503e75d to your computer and use it in GitHub Desktop.
Save ironars/5c62024fe67f6fef4371b0c0a503e75d to your computer and use it in GitHub Desktop.
Write a closure that prints Red and Green alternatively on each call.
const rg = (function () {
let initialValue = 'red';
return function inner() {
console.log(initialValue);
if(initialValue === 'red') {
initialValue = 'green';
} else if (initialValue === 'green') {
initialValue = 'red';
}
}
})();
rg(); // red
rg(); // green
rg(); // red
rg(); // green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment