leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2665.js (690B)


      1 /**
      2  * @param {integer} init
      3  * @return { increment: Function, decrement: Function, reset: Function }
      4  */
      5 
      6 var createCounter = function(init) {
      7   var crnt = init;
      8   return {
      9     increment: () => ++crnt,
     10     decrement: () => --crnt,
     11     reset: () => crnt = init,
     12   }
     13 };
     14 
     15 var createCounter = function(init) {
     16   let currentCount = init;
     17   return new Proxy({}, {
     18     get: (target, key) => {
     19       switch(key) {
     20         case "increment":
     21           return () => ++currentCount;
     22         case "decrement":
     23           return () => --currentCount;
     24         case "reset":
     25           return () => (currentCount = init);
     26         default:
     27           throw Error("Unexpected Method")
     28       }
     29     },
     30   });
     31 };
     32