I am practicing my JavaScript Skills, I have been mastering a simple increment counter function which I seem to have gotten right, so today I wanted to take that script to the next level and decrement the value too,
Here is my issue, when I click add number, then I proceed to click minus number, the first click still adds one value before it is counting down, what am I missing to avoid this from happening?
My code is below:
const output = document.getElementById('counter');
const addBtn = document.getElementById('btn-add');
const minusBtn = document.getElementById('btn-minus');
let number = 0;
function increment() {
output.innerHTML = number++;
}
function decrement() {
output.innerHTML = number--;
}
addBtn.addEventListener('click', increment);
minusBtn.addEventListener('click', decrement);
<div class="container">
<h1>Increment and Decrement Numbers</h1>
<div class="output">
<div>
<p>Your total count is: <span id="counter"></span></p>
</div>
<div class="buttons">
<button id="btn-add">+</button>
<button id="btn-minus">-</button>
</div>
</div>
</div>