5

I've been given a task that I have pretty much solved myself but there is some small correction that needs to be made and I need a help from someone to point me out of where to make some cosmetic changes to my code:

Here is the Task:

Transform the multi-condition if statements sample code into a switch sample code.

var myAge = parseInt(prompt("Enter your age", 30), 10);

if (myAge >= 0 && myAge <= 10) {
  document.write("myAge is between 0 and 10<br />");
}

if (!(myAge >= 0 && myAge <= 10)) {
  document.write("myAge is NOT between 0 and 10<br />");
}

if (myAge >= 80 || myAge <= 10) {
  document.write("myAge is 80 or above OR 10 or below<br />");
}

if ((myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89)) {
  document.write("myAge is between 30 and 39 or myAge is " + "between 80 and 89");

}
<DOCTYPE html>

  <html lang="en">
  <head>
    <title>SOME CODE</title>
  </head>

  <body>
  </body>
  </html>

So the Result by Default under the age 30 is:

myAge is NOT between 0 and 10 myAge is between 30 and 39 or myAge is

between 80 and 89

Here is what I'm done so far:

var myAge = parseInt(prompt("Enter your age", 30), 10); // Get the user's response, converted to a number
switch (true) { // Switch statement if the condition is True
  case myAge >= 0 && myAge <= 10: // Check the inputs value in range 0 - 10                     
    document.write("myAge is between 0 and 10<br/>");
    break;
  case (!(myAge >= 0 && myAge <= 10)): // Check the inputs value if it's not a in range between 0 - 10 ( !(myAge >= 0 && myAge <= 10) )
    document.write("myAge is NOT between 0 and 10<br/>");
    break;
  case myAge >= 80 || myAge <= 10: // Check the inputs value if it's greater/equal to 80 OR less/equal to 10
    document.write("myAge is 80 or above OR 10 or below<br/>");
    break;
  default:
    document.write("myAge is between 30 and 39 or myAge is " + "between 80 and 89"); // Check the inputs value in range 30 - 39 And 80 - 89 

}
<DOCTYPE html>

  <html lang="en">
  <head>
    <title>Chapter 3, Example 2</title>
  </head>
  <body>
  </body>
  </html>

And, as you can see that the result is slightly different. I got printed just this:

myAge is NOT between 0 and 10

I know the solution is simple anough BUT unfortunately I cannot solve it so that it will print a:

myAge is NOT between 0 and 10 myAge is between 30 and 39 or myAge is

as well.

Please, someone, help me so sovle it I would really apreciate it!

4
  • 5
    um, switch would be a very poor choice for ranges. Commented Mar 1, 2018 at 17:13
  • 2
    Remove the break statements because the first example doesn't use else if Commented Mar 1, 2018 at 17:18
  • @epascarello Hello, and thanks for your reply! Do you mean that I have to remove all of them from 3 cases or some of them? And I know that the SWITCH is NOT a good Option but that the only option I got to choose from =) Commented Mar 1, 2018 at 17:32
  • Yes, remove all of them so that the evaluation of the cases won't stop once one of them is meet. Commented Mar 1, 2018 at 17:46

1 Answer 1

1

Nested Switch Case

var myAge = parseInt(prompt("Enter your age", 30), 10);

switch (true) {
  case myAge >= 0 && myAge <= 10:
    document.write("myAge is between 0 and 10<br/>");
  default:
    switch (true) {
      case (!(myAge >= 0 && myAge <= 10)):
        document.write("myAge is NOT between 0 and 10<br/>");
      default:
        switch (true) {
          case myAge >= 80 || myAge <= 10:
            document.write("myAge is 80 or above OR 10 or below<br/>");
          default:
            switch (true) {
              case (myAge >= 30 || myAge <= 39) || (myAge >= 80 && myAge <= 89):
                document.write("myAge is between 30 and 39 or myAge is " + "between 80 and 89");
            }
        }
    }
}

Reasons:

  1. As you have used series of if, not if-else-if, it means we shouldn't use break statements with cases in the Switch
  2. If we don't use break, the control just falls through, so the consecutive case's condition is not checked
  3. Only way to force the condition check, is to use nested Switch statements after default keyword!
Sign up to request clarification or add additional context in comments.

3 Comments

Ooo I see what do you mean! And, as for the newbies like me in JavaScript, I didn't know that I can insert a DEFAULT more than once. Thanks for your reply really appreciate it!
Using ranges in switch is a bad practice. Just use if and else if
I know, @epascarello, The question might be from a coding challenge or just a test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.