Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Why this switch statement isn't work perfectly?

const number = prompt("Enter your number");
const txt = "You result is : ";

switch (number) {
  case (number >= 80 && number <= 100):
    document.write(`${txt} A+`);
    break;
  case (number >= 70 && number <= 80):
    document.write(`${txt} A gread`);
    break;
  case (number >= 60 && number <= 70):
    document.write(`${txt} B gread`);
    break;
  case (number >= 50 && number <= 60):
    document.write(`${txt} C gread`);
    break;
  case (number >= 33 && number <= 50):
    document.write(`${txt} D gread`);
    break;
  case (number >= 0 && number <= 33):
    document.write(`${txt}  Field !`);
    break;
  case (number > 100 || number < 0):
    document.write(`It's not a valid number. Please input any valid number.`);
    break;
  default:
    document.write(`Not input any number. Please input any number .`);
    break;
}

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

With a switch-statement, you evaluate the expression right after switch. In your case that would be number.

So, your cases are evaluated with the number. This is what will happen:

  • Suppose, we enter '88' in the prompt.
  • The first thing you are evaluating number against is (number >= 80 && number <= 100). The result of the latter is true.
  • now the value of number, 88 is compared to true, like this: 88===true.
  • this results in a false, so the next case is evaluated.
  • the same happens for the following cases, right until the point you hit the default-case.
over 4 years ago · Santiago Trujillo Report

0

There are two reasons why this code will not work:

  • The first reason is because the prompt method return a string as you can see by typing

      console.log('Type of number:', typeof number);
      // Expected output: Type of number: string
    

    You can resolve this by parsing the output of the prompt method as follows

      const number = parseInt(prompt("Enter your number"));
    
  • The second reason is because in a switch statement statement, the evaluated value of the switch expression is compared the the evaluated values of the cases.

    It means that the value of number (number) is compared to the expression (number >= 80 && number <= 100) (comparison expression) and so on with the others.

    So unless one of yoru case expression yield a number the switch will try to evaluate the value of number to the expression (which is always true) meaning that the switch is comparing 45 === true, not matching any of the cases.

Just use if/else instead:

if (number >= 80 && number <= 100)
    document.write(`${txt} A+`);
else if (number >= 70 && number <= 80)
    document.write(`${txt} A gread`);
else if (number >= 60 && number <= 70)
    document.write(`${txt} B gread`);
else if (number >= 50 && number <= 60)
    document.write(`${txt} C gread`);
else if (number >= 33 && number <= 50)
    document.write(`${txt} D gread`);
else if (number >= 0 && number <= 33)
    document.write(`${txt}  Field !`);
else if (number > 100 || number < 0)
    document.write(`It's not a valid number. Please input any valid number.`);
else
    document.write(`Not input any number. Please input any number .`);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!