#2665: Counter II

jd2r

DR

Posted on May 16, 2023

#2665: Counter II

Number 3 on the 30 Days Challenge is #2665, Counter II. This challenge is primarily an add-on to the last day, so be sure to check out my post on #2620 for concept detailing.

Process

The instructions given are pretty simple.

Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.

The three functions are:

increment() increases the current value by 1 and then returns it.
decrement() reduces the current value by 1 and then returns it.
reset() sets the current value to init and then returns it.

There are also some straightforward tests included.

const counter = createCounter(5)
counter.increment(); // 6
counter.reset(); // 5
counter.decrement(); // 4
Enter fullscreen mode Exit fullscreen mode

We're basically dealing with a glorified version of the previous challenge. All we're gonna do is write it the same way, but with reset and decrement functions added. Both are simple to implement, but we'll start with what we have.

Let's write a function that has the basic functionality and the increment function included in a return object.

function createCounter(init) {
  let i = 0;
  return {
    increment: () => init + ++i
  }
}
Enter fullscreen mode Exit fullscreen mode

We declare a private variable i and return an object with an increment function that increases i by 1 and returns the new value.

But in the last challenge you used i++, not ++i. What's the deal?

It's because in the last problem, we needed the counter to return init the first time it was called. For this function, we need it to always return init plus an incremented value of i. Since ++i returns the new value, it's just like adding 1 the first time around, 2 the second, etc.

The decrement logic will use an almost identical structure.

function createCounter(init) {
  let i = 0;
  return {
    increment: () => init + ++i,
    decrement: () => init + --i
  }
}
Enter fullscreen mode Exit fullscreen mode

This should make sense if you understand that --i is the same as ++i, it just works in the opposite way.

Now we write the reset function, which we're told sets the value that will be returned back to init. How did we accomplish this the first time? By setting i back to 0.

function createCounter(init) {
  let i = 0;
  return {
    increment: () => init + ++i,
    decrement: () => init + --i,
    reset: () => {
      i = 0;
      return init;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

And that's the solution.

Conclusion.

Incrementing operators can be confusing. Once you learn the cheatsheet for how they work, the solution to the problem begins to make sense.

i++ - increments i then returns i - 1
++i - increments i then returns i
i-- - decrements i then returns i + 1
--i - decrements i then returns i

If you found this helpful, be sure to leave a πŸ’– on the post and a ⭐ on the Github repo!

Follow for more LC content :)

πŸ’– πŸ’ͺ πŸ™… 🚩
jd2r
DR

Posted on May 16, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related