How to make a simple counter function in JavaScript that increments on a condition

Advertisements

I am currently working on a simple javascript counter that should have a variable named "seq" set to 0 on page load, and increments every time a button is clicked. I’ve created a function of what I think it should look like, but am not sure if it is the right approach. Any suggestions? Thanks.

 var seqcounter = function () {
             seq = 0;

            document.getElementById("myBtn").addEventListener("click", function () {
                seq++;
            });


        }

>Solution :

Mybe better to write this way :

var seq = 0;
var increaseSeq = function (){
seq++;
}

document.getElementById("myBtn").addEventListener("click", function () {
increaseSeq();
});

Leave a Reply Cancel reply