Reset counter to 0 if value from function parameter is different in javascript?

I am sending value dynamically in counter function i.e, button can be counter(0), counter(1),counter(2), etc.

Now say if count = 0 (i.e, counter(0)), then I want to increment global variable i.

And now count = 1, then reset the global variable i to 0 and again start incrementing from first.

Again, say if button counter is 0 again and for counter 0 last increment was 4, then it must start from 5.

How can I do that in javascript. Is it possible to do so?

var i =0;
function counter(count){
i++;
}
<button type="button" onclick=counter(0)>Change counter</button>
var count = 0;
function myfunction() {
         <button type="button" onclick="counter(` + count + `)" class="btn btn-primary rounded-0 float-right">Change Counter</button>
         count++;
                }

49 thoughts on “Reset counter to 0 if value from function parameter is different in javascript?”