Implement a make counter function
The makeCounter problem is the classic interview question when it comes to showcasing closures ability to do data hiding and encapsulation. It’s also the best example, using which we can explain closures.
1. Basic Implementation
Implement a makeCounter function which returns a function, which when invoked increment the initial value by a step value provided.
2. Counter with state utilities implementation
Implement a makeCounter utility function which ability to increment, decrement, set, reset and get values of the counter.
Note: There can also be a requirement to implement a min and max limit values on the counter. This is very straight forward, where before changing the counter values, we can simply run a check and limit the value in case it overflows or underflows the max and min values.
Further Reading
I strongly encourage you to explore and tackle additional questions in my Closure Questions for Frontend Interviews blog series.
By doing so, you can enhance your understanding and proficiency with closures, preparing you to excel in your upcoming frontend interviews.
Wishing you best. Happy Interviewing 🫡.