Counter with Private State
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Write createCounter(start), where start defaults to 0. Return an object with exactly four methods and no other properties: increment() adds 1 and returns the new count, decrement() takes 1 away and returns the new count, reset() sets the count back to start and returns it, and value() returns the current count. Keep the count private in a closure, so nothing outside can read or change it except through these methods, and each counter keeps its own count.
Your code must pass
- Counts up and down from the start
- Starts at 0 when no start is given
- reset() goes back to the start
- Each counter keeps its own count
- Has only the four methods, with the count kept private