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
Mannix
Mannix4mo ago
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 switch
ἔρως
ἔρως4mo ago
https://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:
button.onclick = function(){
...
};
button.onclick = function(){
...
};
and this:
<div class="container">
...
</div>
<div class="container">
...
</div>
use this:
<form id="form" class="container">
...
</form>
<form id="form" class="container">
...
</form>
and this:
const form = document.getElementById('form');

form.addEventListener(function(event) {
event.preventDefault();
...
});
const form = document.getElementById('form');

form.addEventListener(function(event) {
event.preventDefault();
...
});
ilyes
ilyesOP4mo ago
thnks
ἔρως
ἔρως4mo ago
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
Want results from more Discord servers?
Add your server