3. ...And The Good!


#1



https://www.codecademy.com/en/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661#


Replace this line with your code. 
//can anyone help me understand why this code is not working!
var i = 1;

while( i < 21){
    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);
    } 
    i++;
}


#2

@saad79,

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] =comparing= it to the number Value 0
which will result in a Boolean VALUE

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

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

 switch(true) {
    case (numbers[x] % 3 === 0 && numbers % 5 === 0):
        console.log("FizzBuzz");
        break;
    case ( numbers[x] % 3 === 0):
        console.log("Fizz");
        break;
    case (numbers[x] % 5 === 0):
        console.log("Buzz");
        break;
    default:
       console.log( numbers[x] );
 }

#3

Thank you !
Your reply was extremely helpful and it cleared the confusion I had regarding the switch statements !