masha2
March 9, 2024, 2:24pm
1
const input = document.getElementById("number")
const btn = document.getElementById("convert-btn")
const output = document.getElementById("output")
btn.addEventListener("click", correctInput)
const correctInput = (num)=>{
if(num === 0){
output.innerText = "Please enter a valid number"
}
}
why is the text not displayed?
the condition is not true, so the text can not be displayed
masha2:
if(num === 0){
masha2
March 9, 2024, 2:38pm
3
the fact is that from all the training I have undergone, I don’t know any other answer)
masha2
March 9, 2024, 2:52pm
4
const input = document.getElementById("number")
const btn = document.getElementById("convert-btn")
const output = document.getElementById("output")
btn.addEventListener("click", correctInput)
const correctInput = ()=>{
const number = input.value
if(!number){
output.textContent = "Please enter a valid number"
return
}
}
this also not work
masha2:
if(!number)
this condition doesn’t make sense, if you want to prevent the user from submitting an empty input you can say
if (input.value === "") {
output.textContent = "Please enter a valid number";
return;
}
you’re if condition is waiting for the condition !input.value to be true, so it can execute the code inside.
( ! )not operator is useful when you want to toggle between two opposite values . (true/false), (1/0) …
happy coding.
You can’t access correctInput
before its definition when that function is declared using const
or let
and function expressions are not hoisted. Move the event listener to after the function definition or define a normal function declaration using the function
keyword.
JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables, classes, or imports to the top of their scope, prior to execution of the code.
@mohamedelmir An empty string is a falsy value.
1 Like
that’s true, thank you for the correction and the mention
masha2
March 11, 2024, 7:11pm
8
Why does strict equality not work in conditions?
else if(input.value === 1023){
output.innerText = "MXXIII"
return
}
Because it is a string. The input
element value is always a string.
'42' == 42
true
'42' === 42
false
1 Like
system
Closed
September 10, 2024, 8:39am
10
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.