Counter Function

by Ryan Morris

JavaScript

/**
 * Counter Function
 * 
 * Make a function that can keep track of a count value
 * And allows you to increment or decrement it while retaining the value
 *
 * The value starts at 0 and will increment by 1 
 *
 * count.value(); // 0 -- starts at 
 * count.up(); // 1
 * count.up(); // 2
 * count.down(); // 1
 * count.value(); // 1
 *
 */

var count = (function() {
 var count = 0;
  return {
    value: function () {
      return count;
    },
    up: function() {
      count++;
    },
    down: function () {
      count--;
    }
  }
})();

console.assert(count.value() === 0);
count.up();
count.up();
count.up();
console.assert(count.value() === 3);
count.down();
console.assert(count.value() === 2);