Created
July 26, 2014 06:33
-
-
Save hannaliebl/dcc0879046f2bb1f577a to your computer and use it in GitHub Desktop.
Clarissa's closure example exercise solution
This file contains 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
function makeCounter () { | |
var counter = 0; | |
var commands = []; | |
return function (str) { | |
if (str === "inc") { | |
counter = counter + 1; | |
commands.push("inc"); | |
return counter; | |
} else if (str === "dec") { | |
counter = counter - 1; | |
commands.push("dec"); | |
return counter; | |
} else if (str === "ops") { | |
return commands.length; | |
} else { | |
return counter; | |
} | |
} | |
} | |
var counter = makeCounter(); | |
console.log(counter()); | |
console.log(counter("inc")); | |
console.log(counter("dec")); | |
console.log(counter("ops")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment