Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Created January 11, 2016 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmloveaa/62cef59b4f98e427a4fd to your computer and use it in GitHub Desktop.
Save mmloveaa/62cef59b4f98e427a4fd to your computer and use it in GitHub Desktop.
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