java script switch
hello im new to js and following a tutorial on switch js ,i made a code that tell you which day you are in by entering number and i added a default which says this is not a day whenever you enter a number not listed ,for some reason when i enter any listed number like 1 it says "its not a day" could you please help me debug my code .
https://codepen.io/ilyas-draissia/pen/XWLaJeJ?editors=1010
4 Replies
because the value you get from the input is a string and you compare it to a number in your switch
so you either convert the value to a number with
Number()
or parseInt()
method or change number to strings in your switchhttps://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/valueAsNumber <-- just use this
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number <-- with this
instead of this:
and this:
use this:
and this:
thnks
by the way, you have a global variable
don't use those
that
let day;
should be inside the event handler
you can even go deeper and use an array for the day names