Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript switch case with logical and

I am trying to write a switch statement but it doesn't seem to work how I want.

getExerciseDescription(exerciseId, intensity_level){

    alert(exerciseId + " " + intensity_level)

    switch (exerciseId && intensity_level) {
        case (1 && 1):
        this.header="Exercise 1 Level 1";
        this.instructions="Exercise 1 Level 1";
        break;
        case (1 && 2):
        this.header="Exercise 1 Level 2";
        this.instructions="Exercise 1 Level 2";
        break;  


        case (2 && 1):
        this.header="Exercise 2 Level 1";
        this.instructions="Exercise 2 Level 1";
        break;  
        case (2 && 2):
        this.header="Exercise 2 Level 2";
        this.instructions="Exercise 2 Level 2";
        break;

        default:
        this.header="Default";
        this.instructions="Default";
        break;
    }

    return new Popup(this.header, this.instructions);
} 

The alerts gives 2 and 1 but the returned value is for (1 && 1). Why is it so? How can I fix this?

like image 228
Thinker Avatar asked Mar 05 '17 18:03

Thinker


2 Answers

You just can't use a switch like that. (1 && 1) == (2 && 1) == 1 and (1 && 2) == (2 && 2) == 2, so you're doing the equivalent of:

getExerciseDescription(exerciseId, intensity_level){

    alert(exerciseId + " " + intensity_level)

    switch (exerciseId && intensity_level) {
        case (1):
        this.header="Exercise 1 Level 1";
        this.instructions="Exercise 1 Level 1";
        break;
        case (2):
        this.header="Exercise 1 Level 2";
        this.instructions="Exercise 1 Level 2";
        break;  


        case (1):
        this.header="Exercise 2 Level 1";
        this.instructions="Exercise 2 Level 1";
        break;  
        case (2):
        this.header="Exercise 2 Level 2";
        this.instructions="Exercise 2 Level 2";
        break;

        default:
        this.header="Default";
        this.instructions="Default";
        break;
    }

    return new Popup(this.header, this.instructions);
} 

So of course the lower two cases will never execute. You're better of just using if and else if statements, or maybe nested switches if you want.

You could also do something like:

switch (exerciseId + " " + intensity_level) {
    case("1 1"): ...
    case("1 2"): ...
    case("2 1"): ...
    case("2 2"): ...
like image 120
Charles Clayton Avatar answered Oct 07 '22 11:10

Charles Clayton


That isn't how a switch statement will evaluate. For your scenario it will always evaluate to the 2nd integer in the logical and &&.

(More information about Logical Operators)

Logical AND (&&)

expr1 && expr2

Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

You actually don't even need to use a switch, you can write it with a simple if

if (exerciseId <= 2 && intensity_level <= 2){
    this.header=`Exercise ${exerciseId} Level ${intensity_level}`;
    this.instructions=`Exercise ${exerciseId} Level ${intensity_level}`;
} else {
    this.header="Default";
    this.instructions="Default";
}
like image 24
Christopher Moore Avatar answered Oct 07 '22 12:10

Christopher Moore