How to make a 3 case switch statement in javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to make a 3 case switch statement in javascript?

#bool1 #bool2 #bool3 #int # switch(true){ Case1: int = 0 Case2: int = 1 Case3: int = 2 } If all bool are true Then output must be 0 1 2 0 1 2 # If any two bool are true then switching must be like 1 2 1 2 (bool2 and bool3 are true and bool1 is false ) #If one bool true Then no switch statement should execute Let's discuss 😸 I've made few logic gates too but they ain't working as i needed

27th Jun 2022, 3:11 AM
Shubh
Shubh - avatar
17 Answers
0
Shubh is this your actual output correct ? mode is start mode, execute=6 true true true mode = 0 1,2,0,1,2,0 true true false mode = 0 1,0,1,0,1,0 true false true mode = 0 2,0,2,0,2,0 true false false mode = 0 0,0,0,0,0,0 false true true mode = 1 2,1,2,1,2,1 false true false mode = 1 1,1,1,1,1,1 false false true mode = 2 2,2,2,2,2,2 false false false mode = 0 0,0,0,0,0,0
27th Jun 2022, 11:27 PM
zemiak
+ 4
Shubh Java or JavaScript?
27th Jun 2022, 6:15 AM
A͢J
A͢J - avatar
+ 2
A͢J offcourse javascript I'd mentioned there already
27th Jun 2022, 6:18 AM
Shubh
Shubh - avatar
+ 2
Shubh You have mentioned Java also in tag. There is no int in JavaScript, there is only var
27th Jun 2022, 6:38 AM
A͢J
A͢J - avatar
+ 2
Shubh If bool1 and bool2 are true and bool3 is false then what should be the output?
27th Jun 2022, 6:53 AM
A͢J
A͢J - avatar
+ 2
Shubh As switch accept only one value at a time so you can call switch 2 times: Note : switch don't accept boolean value. Here is a one solution just for hint var bool1 = true var bool2 = true var bool3 = false function print(x) { switch (x) { case 0: console.log(0); case 1: console.log(1); case 2: console.log(2); } } if (bool1 && bool2 && bool3) { print(0) print(0) } if (bool1 && bool2 && !bool3 || bool2 && bool3 && !bool1) { print(1) print(1) }
27th Jun 2022, 6:59 AM
A͢J
A͢J - avatar
+ 1
In hurry i tagged java By int i meant integer
27th Jun 2022, 7:09 AM
Shubh
Shubh - avatar
+ 1
If any two boolean are true then output should be Like 1 0 1 0 1 0
27th Jun 2022, 7:10 AM
Shubh
Shubh - avatar
+ 1
var bool1 = false; var bool2 = true; var bool3 = false; var execute = 10; var mode = null; if(mode==null) { switch(true) { case (bool1): mode = 0; break; case (bool2): mode = 1; break; case (bool3): mode = 2; break; default: mode = 0; break; } } for(var x = 0;x<execute;x++) { switch(true){ case(bool1 && bool2 && bool3): switch(mode){ case 0: mode = 1; console.log(mode); break; case 1: mode = 2; console.log(mode); break; case 2: mode = 0; console.log(mode); break; }; break; case(bool3 && bool2): switch(mode){ case 1: mode = 2; console.log(mode); break; case 2: mode = 1; console.log(mode); break; }; break; case(bool3 && bool1): switch(mode){ case 0: mode = 2; console.log(mode); break; case 2: mode = 0; console.log(mode); break; }; break; case(bool1 && bool2): switch(mode){ case 1: mode = 0; console.log(mode); break; case 0: mode = 1; console.log(mode); break; }; break; default: console.log(mode); break; } }
27th Jun 2022, 7:11 AM
Shubh
Shubh - avatar
+ 1
I made this
27th Jun 2022, 7:11 AM
Shubh
Shubh - avatar
+ 1
Just a little optimization is needed
27th Jun 2022, 7:11 AM
Shubh
Shubh - avatar
+ 1
Here
27th Jun 2022, 7:13 AM
Shubh
Shubh - avatar
+ 1
zemiak exactly that's the output i want.. Just take an example of mode switching in a gun If it has auto then 0 Single then 1 And burst then 2 As some have all 3 modes Some have only two modes like single n burst for handguns N some have only one mode So am creating an gun script for my game engine N just wanting the code to be as optimised as it could
28th Jun 2022, 2:28 AM
Shubh
Shubh - avatar
0
Each bool holds a different represention if an exterior function
27th Jun 2022, 5:54 PM
Shubh
Shubh - avatar
0
zemiak here is the logic gate i got
28th Jun 2022, 2:29 AM
Shubh
Shubh - avatar