V
V
Vit132021-06-17 15:23:01
JavaScript
Vit13, 2021-06-17 15:23:01

The problem with the switch-case construct. How to decide?

I’ve been sitting here for an hour now, I can’t understand what the problem is

======================================= ===========

let chs = prompt('Enter a number between 1 and 31');
switch(chs) {
case (chs < '10' && chs > '0'):
alert('You are in the top ten');
break;
case (chs > '20' && chs < '10'):
alert('You are in the second ten');
break;
case (chs > '30' && chs < '20'):
alert('You are in the top ten');
break;
default:
alert('This number is not supported');
break;

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DaBags, 2021-06-17
@Vit13

To do this, you need to replace the line:
switch(chs) {
with: And also pay attention to the comparison signs in the conditions
switch(true) {

W
WapSter, 2021-06-17
@wapster92

Switch is very slow with conditions, use if or explicitly set the value of the variable. If, as you want, then in switch you need to pass true and not a variable, then case will be executed, which will be true

K
Kovalsky, 2021-06-17
@lazalu68

The problem is that prompt will not return a number. And you are not comparing with numbers: '10' is obviously not a number

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question