r/JavaScriptTips • u/HeadlineINeed • May 21 '22
Trying to learn switch statement, having issue with rational operators
I am trying to convert a if, else if and else statement into a switch statement. Following along with MDN; https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Conditionals#conditionals_3
I can’t for the life of my figure out switch statements
2
u/zsmaynard May 22 '22 edited May 22 '22
That's a tough example to convert into a switch statement because of the way switch statements behave.
Why don't we first look at the different pieces of a switch statement:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}You may be running into some issues because of the expression you've passed in. Let's assume you've passed the variable of machineActive into the switch statement as the expression.
Since the expression is looking at a datatype of boolean, it's going to expect each of your cases to evaluate as booleans. So, if machineActive = true, the switch statement is going to expect each case to evaluate to a value of true. If machineActive = false, it will expect each case to evaluate to a value of false.
the value we give to the switch statement as the expression is used as the basis to compare against. You could also turn this around and pass switch (false) {...} and have the desired expressions evaluate to false instead of true.
let score = 15;
let machineActive = true;
switch (machineActive) {
case score < 0 && score > 100:
console.log('Not possible, an error has occurred');
break;
case score >=0 && score <= 19:
console.log('That was a terrible score - total fail');
break;
...
...
...
}In the example above, the variable of machineActive holds a boolean value of true. Since a boolean of true has been passed into the switch statement as the expression, it's going to look for each case to evaluate to true. So in this example, the second case is what should evaluate as true, so the console.log statement should print out, 'That was a terrible score - total fail'.
Once a condition has been evaluated as true, code will run, and then the break statement will kick you out of the switch statement and stop it from running.
You can think of the default statement as the “catch all”, or the else. If none of the cases evaluate to true (or match what’s been passed into the switch as the expression), the default will be ran.
1
u/Mr-Bitter May 21 '22
I don't believe so. Just put it in parenthasis if I'm not mistaken. Unfortunately I'm on my phone atm, so I can't write test code.
1
u/bobbyv137 May 22 '22 edited May 22 '22
switch statements are one of the first things that many courses teach when learning JS. Funny thing is you end up learning so much other stuff that it's easy to forget the basics.
I think this is the kind of thing you're looking for. Hopefully it'll make sense
You could also have the switch conditional based on machineActive, and if it's false handle it accordingly.
1
u/Mr-Bitter May 21 '22
I understood it by thinking of it as a "when value equals this" instead of "if value equals this".