Last active
January 8, 2020 12:11
-
-
Save ironars/5c62024fe67f6fef4371b0c0a503e75d to your computer and use it in GitHub Desktop.
Write a closure that prints Red and Green alternatively on each call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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