javascript fizzbuzz switch statement

前端 未结 8 724
慢半拍i
慢半拍i 2021-01-18 00:13

I\'m currently taking the code academy course on Javascript and I\'m stuck on the FizzBuzz task. I need to count from 1-20 and if the number is divisible by 3 print fizz, by

8条回答
  •  长发绾君心
    2021-01-18 00:42

    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)

提交回复
热议问题