FizzBuzz switch statement


#1

why does using a switch statement not work on this problem?
for(var i=1; i<21; i++){
switch(i){
case i%3 === 0 && i%5 === 0:
console.log("FizzBuzz");
break;
case i%3 === 0:
console.log("Fizz");
break;
case i%5 === 0:
console.log("Buzz");
break;
default:
console.log(i);
break;

}

};


#2

@cssplayer30735,

In
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
[quote]

switch (parameter-expression) {
  case value1:
    //Statements executed 
    //      when the VALUE of expression matches value1
    [break;]
  case value2:
     //Statements executed 
    //      when the VALUE of expression matches value2
    [break;]
 ...
  case valueN:
    //Statements executed 
    //      when the VALUE of expression matches valueN
    [break;]
  default:
    //Statements executed 
    //      when none of the VALUE's match the VALUE of the expression
    [break;]
}

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

The switch statement evaluates an parameter-expression,
matching the parameter-expression's VALUE to a case clause,
and executes statements associated with that case.
( and with the break or return-statement will exit the switch statement )
[end-quote]

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

In your case clause you are using an expression
An expression is any valid unit of code that resolves to a VALUE.

Conceptually, there are two types of expressions:
-1 those that assign a value to a variable
and
2- those that simply resolve to a VALUE.

So in your case clause you are using the 2nd kind of expression
and you do that by
using the remainder operator on numbers[x]
which will result in a number VALUE

Thus your case clause VALUE
is matched against the VALUE of the parameter-expression

So if you use a number VALUE of 0 as
being the parameter-expression's VALUE
would get the -result- we want:

for (var x=0; x < numbers.length; x++) { 
 switch(0) {
    case (numbers[x] % 3 && numbers % 5):
        console.log("FizzBuzz");
        break;
    case ( numbers[x] % 3):
        console.log("Fizz");
        break;
    case (numbers[x] % 5):
        console.log("Buzz");
        break;
    default:
       console.log( numbers[x] );
 } 
// console.log(numbers[x]);
 }