javascript - How to run two cases with different functions at the same time? -
i know possible run multi cases if same action. considering example below, if true , b, something...
switch(true){ case a: case b: //do something; break; case c: //do else;}
however, not sure how make work following:
switch(true){ case a: alert("a"); case b: alert("b"); break; case c: //do else;}
if true both , b, should alert "a" , "b". probably, switch statement not option example, not figure out. suggestion, recommendation?
i think you're trying use switch
incorrectly, looks you're trying check if a
, b
, c
true , if run matching alert()
. purpose of switch
check value being given (so 1 case can true) , run it, so:
function demofunction1(fruit){ var sentence = ""; switch (fruit) { case "apple": sentence = "i love apples!"; break; case "banana": sentence = "i hate bananas!"; break; case "watermelon": sentence = "watermelon okay"; break; } alert(sentence); }
so checking value of fruit
is, if it's "apple"
, "banana"
or "watermelon"
run matching case's code.
it looks me wanna use if statements so:
function demofunction2(a, b, c){ if (a) { alert("a"); } if (b) { alert("b"); } if (c) { alert("c"); } }
this way you're checking if a
, b
, c
true
individually , if of them what's in {}
of if statement run (such function calls or in case alerts). work combination of a
, b
, c
being true
/false
.
Comments
Post a Comment