Hone logo
Hone
Problems

Create a Versatile Counter Function in JavaScript

This challenge asks you to implement a flexible counter function in JavaScript. A counter is a fundamental concept in programming, used for tracking iterations, managing state, and more. Your goal is to create a function that can be initialized with a starting value and incremented or decremented as needed, providing a clean and reusable way to manage numerical values.

Problem Description

You need to create a JavaScript function called createCounter that returns an object with two methods: increment() and decrement().

  • createCounter(initialValue): This function takes an initialValue (a number) as an argument and returns an object.
  • increment(): When called on the returned object, this method should increase the counter's internal value by 1 and return the new value.
  • decrement(): When called on the returned object, this method should decrease the counter's internal value by 1 and return the new value.

The counter should maintain its state between calls to increment() and decrement(). The initialValue should be used to set the starting value of the counter.

Key Requirements:

  • The function must return an object with increment() and decrement() methods.
  • The increment() method must increase the counter by 1 and return the new value.
  • The decrement() method must decrease the counter by 1 and return the new value.
  • The counter's state must be preserved between calls.
  • The initialValue must be correctly set when the counter is created.

Expected Behavior:

The returned object should behave as a counter, maintaining its internal state and updating it correctly with each call to increment() or decrement().

Edge Cases to Consider:

  • initialValue being zero.
  • initialValue being a negative number.
  • Repeated calls to increment() and decrement().

Examples

Example 1:

Input: createCounter(0)
Output: { increment: [Function: increment], decrement: [Function: decrement] }
Explanation: Creates a counter initialized to 0. The output is the object returned by createCounter.

Example 2:

Input: let counter = createCounter(5); counter.increment(); counter.increment();
Output: 7
Explanation: Creates a counter initialized to 5. Incrementing it twice results in a final value of 7.

Example 3:

Input: let counter = createCounter(-2); counter.decrement();
Output: -3
Explanation: Creates a counter initialized to -2. Decrementing it once results in a final value of -3.

Example 4:

Input: let counter1 = createCounter(10); let counter2 = createCounter(20); counter1.increment(); counter2.decrement();
Output:
counter1.increment() returns 11
counter2.decrement() returns 19
Explanation: Demonstrates that each counter object maintains its own independent state.

Constraints

  • initialValue will always be a number.
  • The increment() and decrement() methods should return a number.
  • The function should be efficient and avoid unnecessary computations.

Notes

  • Consider using closures to encapsulate the counter's internal state. This is a common and effective way to manage state in JavaScript.
  • Think about how to ensure that the increment() and decrement() methods have access to the counter's current value.
  • Focus on creating a clean, readable, and maintainable solution.
Loading editor...
javascript