commit e308ff16ba6fe3f558eef1c9312e54030afecf30
parent f9006fddb8765d12f093a36d8e54c2e1c6aa0d3d
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Sun, 7 May 2023 12:16:49 +0200
JavaScript Challenge: Day 3
Diffstat:
2 files changed, 33 insertions(+), 0 deletions(-)
diff --git a/Problems/2665.js b/Problems/2665.js
@@ -0,0 +1,32 @@
+/**
+ * @param {integer} init
+ * @return { increment: Function, decrement: Function, reset: Function }
+ */
+
+var createCounter = function(init) {
+ var crnt = init;
+ return {
+ increment: () => ++crnt,
+ decrement: () => --crnt,
+ reset: () => crnt = init,
+ }
+};
+
+var createCounter = function(init) {
+ let currentCount = init;
+ return new Proxy({}, {
+ get: (target, key) => {
+ switch(key) {
+ case "increment":
+ return () => ++currentCount;
+ case "decrement":
+ return () => --currentCount;
+ case "reset":
+ return () => (currentCount = init);
+ default:
+ throw Error("Unexpected Method")
+ }
+ },
+ });
+};
+
diff --git a/README.md b/README.md
@@ -536,4 +536,5 @@ for solving problems.
| 2492 | Medium | [Minimum Score of a Path Between Two Cities](Problems/2492.cpp) |
| 2497 | Medium | [Maximum Star Sum of a Graph](Problems/2497.cpp) |
| 2620 | Easy | [Counter](Problems/2620.js) |
+| 2665 | Easy | [Counter II](Problems/2665.js) |
| 2667 | Easy | [Create Hello World Function](Problems/2667.js) |