javascript fizzbuzz switch statement

前端 未结 8 715
慢半拍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:47

    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.

提交回复
热议问题