javascript fizzbuzz switch statement

◇◆丶佛笑我妖孽 提交于 2019-12-01 18:00:46

Switch matches the x in switch(x){ to the result of evaluating the case expressions. since all your cases will result in true /false there is no match and hence default is executed always.

now using switch for your problem is not recommended because in case of too many expressions there may be multiple true outputs thus giving us unexpected results. But if you are hell bent on it :

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

}

I thought switch too,but no need.

    for (var n = 1; n <= 100; n++) {
  var output = "";  
  if (n % 3 == 0)
    output = "Fizz";
  if (n % 5 == 0)
    output += "Buzz";
  console.log(output || n);
}

Not to too my own horn but this is much cleaner:

var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <= numbers.length; i++) {  
    if (i % 15 === 0) {
        console.log("FizzBuzz");
    } else if (i % 5 === 0) {
        console.log("Buzz");
    } else if (i % 3 === 0) {
        console.log("Fizz");
    } else {
        console.log(i);
    }
};
jamie

The switch(true) part of this statement helped me. I was trying to do a switch statement for fizzbuzz. My solution incorporates the coding style of Rosettacodes - general solution. Most significantly the use of force typing to shorten the primary conditionals. I thought, it was valuable enough to post:

var fizzBuzzSwitch = function() {
  for (var i =0; i < 101; i++){
    switch(true) {
      case ( !(i % 3) && !(i % 5) ):
        console.log('FizzBuzz');
        break;
      case ( !(i % 3) ):
        console.log('Fizz');
        break;
      case ( !(i % 5) ):
       console.log('Buzz');
       break;
      default:
       console.log(i);
    }
  }
}
LeChatBleu

Here's what made it clear for me, might help : It's a misinterpretation of what switch (x){} means.

It doesn't mean : "whenever whatever I put inbetween those brackets is true, when the value of x changes."
It means : "whenever x EQUALS what I put between those brackets"

So, in our case, x NEVER equals x%3===0 or any of the other cases, that doesn't even mean anything. x just equals x all the time. That's why the machine just ignores the instruction. You are not redefining x with the switch function. And what you put inbetween the brackets describes x and x only, not anything related to x.

In short :
With if/else you can describe any condition.
With switch you can only describe the different values taken by the variable x.

Here's a solution incorporating @CarLuvr88's answer and a switch on 0:

let fizzBuzz = function(min, max){
  for(let i = min; i <= max; i++){
    switch(0){
      case i % 15 : console.log('FizzBuzz'); break;
      case i % 3  : console.log('Fizz'); break;
      case i % 5  : console.log('Buzz'); break;
      default     : console.log(i); break;
    }
  }
}

fizzBuzz(1,20)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!