-2

In my program i'm just checking to see if the user changes anything in the front end on my server. While I run the code below, it seems that it only checks the first case and that's it. Iv come to this conclusion because iv I rearranged what is beings checked first, only the first one will be checked. The inside logic works fine as iv tested it while rearranging.

        case("Pizza Slice w/ Topping" || "Small Pie w/ Topping" || "Large Pie w/ Topping":{
            if(it.comboDrink || it.comboSide || !checkToppings(it) ){
                let customErr = new customError('Y U TOUCH ME PIZZA!');
                throw customErr;
            }
            break;
        

For example right now the inside if statement only runs for "Pizza Slice w/ Topping", and not anything else. Now when I put "Small Pie w/ Topping" first in my case, it only works for "Small Pie w/ Topping" and nothing else. Iv rewritten this whole statement in case there was some invisible characters. I have a similar set up checking other things in this switch statement and they work fine.

Furqan_25
  • 19
  • 6
  • 1
    I think this is flagged as an incorrect duplicate, this is the duplicate that you want to look at https://stackoverflow.com/questions/6513585/test-for-multiple-cases-in-a-switch-like-an-or - Don't use `||` in your switch cases, instead have your cases call through – Brett East May 24 '22 at 03:20
  • @BrettEast could also by any chance explain why my code isn’t working? I have 3 different cases in this same switch statement with the same set up but they work fine. I’ll try your solution, but it’s still bugging my why I had the problem – Furqan_25 May 24 '22 at 04:10
  • 1
    Sure, it's because the `case` you're testing against isn't `('something' || 'somethingElse' || 'someOtherThing')` it's test against what that expression equals. Think of it like this, if you said `const test = ('something' || 'somethingElse' || 'someOtherThing')` and then `case (test)` in this case `test === 'something'`. That's why it works with whatever term you put first. You need to write switch statements like this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch – Brett East May 24 '22 at 13:48
  • I see, thank you for taking the time out to explain this :) – Furqan_25 May 25 '22 at 15:03

0 Answers0