Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Closure - Alternates
// 1/5/2016
// write a function that will return a value that alternates
// between a and b
// var alternate = createFunc2()
// alternate() => 'a'
// alternate() => 'b'
// alternate() => 'a'
// alternate() => 'b'
function createFunc() {
var value = 'a';
function increment() {
if (value==='a') {
console.log('a')
value='b'
}
else {
console.log('b')
value='a'
}
}
return increment
}
var print= createFunc()
print();
print();
print();
print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.