It is my first program in Javascript. Is there any lack or a thing to be improved? It simply increases, decreases, or reset the counter either by buttons or arrow keys.
HTML,
<h1 id="number"></h1>
<button class="btn" id="incr">Increase</button>
<button class="btn" id="reset">Reset</button>
<button class="btn" id="decr">Decrease</button>
JS,
let number = 0
const getNumber = document.getElementById(`number`)
const incrButton = document.getElementById(`incr`)
const decrButton = document.getElementById(`decr`)
const resetButton = document.getElementById(`reset`)
getNumber.textContent = number
const incrFunc = function () {
++number
getNumber.textContent = number
if (number > 0) {
getNumber.style.color = `green`
}
}
const resetFunc = function () {
number = 0
getNumber.textContent = number
getNumber.style.color = `gray`
}
const decrFunc = function () {
--number
getNumber.textContent = number
if (number < 0) {
getNumber.style.color = `red`
}
}
incrButton.addEventListener(`keyup`, function (e) {
e.stopPropagation()
//console.log(e.target === document.body)
if (e.code === `ArrowUp`) {
incrFunc()
}
})
document.addEventListener(`keyup`, function (e) {
//console.log(e.target === document.body)
if (e.code === `ArrowUp`) {
incrFunc()
}
})
document.addEventListener(`keyup`, function (e) {
if (e.code === `ArrowRight` || e.code === `ArrowLeft`) {
resetFunc()
}
})
resetButton.addEventListener(`keyup`, function (e) {
if (e.code === `ArrowRight` || e.code === `ArrowLeft`) {
resetFunc()
}
})
document.addEventListener(`keyup`, function (e) {
if (e.code === `ArrowDown`) {
decrFunc()
}
})
decr.addEventListener(`keyup`, function (e) {
if (e.code === `ArrowDown`) {
decrFunc()
}
})
incrButton.addEventListener(`click`, incrFunc)
resetButton.addEventListener(`click`, resetFunc)
decrButton.addEventListener(`click`, decrFunc)
decr.addEventListener(
keyup, ...
looks like it should probably bedecrButton.addEventListener(
keyup, ...
. \$\endgroup\$