Christine
Christine

Reputation: 55

JavaScript using the Switch Statement - Not getting the expected output

I'm quite new at this and have a lot to learn. I'm using the Switch statement in this piece of JavaScript but I'm not getting the expected output as per the document.write lines. Any help, comments and suggestions are appreciated. - Thank you!

            <script type="text/javascript">

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

            switch (myAge)
            {
                case (myAge  >= 0 && myAge <= 10):
                    document.write("myAge is between 0 and 10");
                    break;

                case (!(myAge >= 0 && myAge <=10)):
                    document.write ("myAge is NOT between 0 and 10 <br />");
                    break;

                case (myAge >= 80 || myAge <= 10):
                    document.write ("myAge is 80 or above OR 10 or below <br />");
                    break;

                case (myAge >= 30 && myAge <=39 || (myAge >= 80 && myAge <= 89)):
                    document.write("myAge is between 30 and 39 or myAge is between 80 and 89");
                    break;

                default:
                    document.write("You did not enter a number.  Please enter a number.");
                    break;
            }       

            document.write("<BR>Execution continues here");


        </script>

This is what I wrote just using the 'if'.

        <script type="text/javascript">

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

        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");
        }   


    </script>

This is the sample piece of code using 'switch' that I have to refer to.

<script type="text/javascript">

var secretNumber = prompt("Pick a number between 1 and 5:", "");
secretNumber = parseInt(secretNumber);

switch (secretNumber)
{
case 1:
   document.write("Too low!");
   break;

case 2:
   document.write("Too low!");
   break;

case 3:
   document.write("You guessed the secret number!");
   break;

case 4:
   document.write("Too high!");
   break;

case 5:
   document.write("Too high!");
   break;

default:
   document.write("You did not enter a number between 1 and 5.");
   break;
}
document.write("<BR>Execution continues here");

</script>

Upvotes: 0

Views: 135

Answers (3)

Bojan Petkovski
Bojan Petkovski

Reputation: 6933

You can make a switch that will evaluate only the true statements, it fill find the first true statement (case) if any, or it will show the default. It will use conditional logic to work. So your code to work just add true instead of myAge

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

switch (true) {
    case (myAge >= 0 && myAge <= 10):
        document.write("myAge is between 0 and 10");
        break;

    case (!(myAge >= 0 && myAge <= 10)):
        document.write("myAge is NOT between 0 and 10 <br />");
        break;

    case (myAge >= 80 || myAge <= 10):
        document.write("myAge is 80 or above OR 10 or below <br />");
        break;

    case (myAge >= 30 && myAge <= 39 || (myAge >= 80 && myAge <= 89)):
        document.write("myAge is between 30 and 39 or myAge is between 80 and 89");
        break;

    default:
        document.write("You did not enter a number.  Please enter a number.");
        break;
}

Just a note that using switch like this is a performance overkill. It is better/faster to use if / else statements.

You can read more about switch here Switch and at the bottom you can see this method used (Method two).

Upvotes: 1

cdosborn
cdosborn

Reputation: 3449

You need to rethink the logic in the case expression.

Let me make a substitution in your code, to illustrate the error

var test = myAge >= 0 && myAge <= 10;
if (test) {
   ...
} else if (!test) {
   ...
} else {
   // WILL NEVER BE RUN
}

If you want to use a switch statement you have to understand the types behind the data you're using.

true : Boolean
false : Boolean
9 : Number
1.0 : Number

In the example below, v must have the same type as value1 and value2.

switch(v)
{
   case (value1):
   ...       
   case (value2):
   ...
}

The issue is the following types don't match in your example.

v = Number
value1 = Boolean
value2 = Boolean

Upvotes: 3

yarden
yarden

Reputation: 1936

for conditions like this, use if:

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

}else if (!(myAge >= 0 && myAge <=10)){
    //...
}

switch is for multiple exactly values, that isn't your situation:

switch(myAge){
    case 0:
        document.write("myAge is *exactly* 0");
        break;
    //...
}

Upvotes: 1

Related Questions