-3
When the values of the inputs are zeroed, the right one was to appear a alert
and a change in the div
, but the page hangs and I can no longer put numbers or click the button.
I realized that when I take the .lenght
the code works.
Why the lenght
hangs the page?
function tabuada(){
let ini=document.getElementById('txt1')
let fim=document.getElementById('txt2')
let passo=document.getElementById('txt3')
let res=document.getElementById('res')
if (ini.value.lenght== 0 || fim.value.lenght== 0 || passo.value.lenght== 0){
window.alert('[ERRO], dados faltando...')
res.innerHTML='Impossível de contar...'
}
else{
res.innerHTML='Contando:'
let i=Number(ini.value)
let f=Number(fim.value)
let p=Number(passo.value)
if(i<f){
//Contagem Crescente
for(let c=i; c<=f; c += p){
res.innerHTML+=`${c}\u{1F449}`
}
res.innerHTML+=`\u{1F3C1}`
}
else{
//Contagem Regressiva
for(let c=i; c>=f; c-=p){
res.innerHTML+=`${c}\u{1F449}`
}
res.innerHTML+=`\u{1F3C1}`
}
}
}
The
lenght
is wrong. Right is rightlength
. Try it like this:if (!+ini.value.trim() || !+fim.value.trim() || !+passo.value.trim()){
– Sam
Samzon present !
– Gato de Schrödinger
@Sam Whereas an empty string is considered
false
, nor needed to use the+
to convert to number:if (!ini.value.trim() || !fim.value.trim() || !passo.value.trim())
. By the way, converting to number can be a problem, because the string"000"
is converted to the zero number (that is, even if it is not empty, it will enter theif
).– hkotsubo
Unless, of course, he also wants to validate that the value is zero (which was not clear in the question). Anyway...
– hkotsubo
@hkotsubo That’s right. He wants to check whether it is empty or zero to fire the Alert.
– Sam