Solved LeetCode challenge 2665 — Counter II with JavaScript

Day 4 of #30DaysOfJavaScript on LeetCode Today's challenge: 2665 — Counter II 🔁 The task was to create a createCounter function that accepts an initial integer init and returns an object with three functions: increment() → increases the value by 1 decrement() → decreases the value by 1 reset() → resets the value back to init Here’s my solution 👇 var createCounter = function(init) { let cache = init; return { increment: function() { return ++init; }, reset: function() { init = cache; return cache; }, decrement: function() { return --init; } }; }; This challenge was a good exercise in understanding closures and object methods — it’s cool how functions can maintain state in JavaScript! 💡 If you’d like to try it out, check it here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #Closures #30DaysOfCode #Functions #Objects #Programming

  • No alternative text description for this image

“You just earned a new follower! Let’s keep inspiring each other 💫🔥” Anubama I

To view or add a comment, sign in

Explore content categories