Skip to content

Instantly share code, notes, and snippets.

@hannaliebl
Created July 26, 2014 06:33
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 hannaliebl/dcc0879046f2bb1f577a to your computer and use it in GitHub Desktop.
Save hannaliebl/dcc0879046f2bb1f577a to your computer and use it in GitHub Desktop.
Clarissa's closure example exercise solution
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