can anyone explain why my switch statement is printing out default?

can anyone explain why my switch statement is printing out default?
var x = 30;
switch(x) {
case x % 3 ===0 && x % 5 ===0:
(result = "fizzbuzz");
break;
case x % 3 ===0:
(result = "fizz");
break;
case x % 5 ===0:
(result = "buzz");
break;
default:
console.log("couldn't find the answer")
}
You already invited:

TimDaub

Upvotes from:

That's not the way a switch works. The expression "x % 3 ===0" returns true, but your variable in the switch (x) is 30, so the condition is not correct

Denis

Upvotes from:

The switch statement expression which is (x%3===0 && x%5===0) returns true, therefore if we replace the whole statement to the boolean expression returned by the statement, we get

switch (x) {
case true:
(result = "fizzbuzz"); break;
}

and since x equals 30, and not equals to true therefore the condition doesn't match and passes by until default is hit (since x%3===0 and x%5===0 conditions also yield true).

If you wanna answer this question please Login or Register