-1
I am creating a calculator and on my console I only get the error:
Uncaught TypeError: number[i] is undefined.
The code returns the expected working, but this error persists.
Follows the code:
let number = document.getElementsByClassName('number')
let calAction = function(){
console.log('work')
}
function getNumberLoad(number){
for(let i = 0; i <= number.length; i++)
number[i].addEventListener('click', calAction, false)
}
document.addEventListener('DOMContentLoaded', getNumberLoad(number), false)
Pq to TAG
brasil
?– tvdias
The number of loops in the for is wrong when using
<=
you have defined one item more than the limit of found elements, the count is from 0, so the length has to be less and not less-or-equal. Should befor(let i = 0; i < number.length; i++)
– Guilherme Nascimento