简体   繁体   中英

Why doesn't this FizzBuzz case statement work?

When I call the function fizzBuzz(arr), the console is just printing the numbers 1..100. Why is the switch statement not working?

<script>


    arr = new Array(100);

    for (var i = 0; i < arr.length; i++) { 
      arr[i] = i + 1
    }

    console.log(arr)

    function fizzBuzz(array) {
      for (var a in array) {
        switch(array[a]) {
          case (array[a] % 5 == 0 && array[a] % 3 == 0):
            console.log('FizzBuzz')
            break;
          case (array[a] % 5 == 0):
            console.log('Buzz')
            break;
          case (array[a] % 3 == 0):
            console.log('Fizz')
            break;
          default: 
            console.log(array[a])
        }
      }
    }

    </script>

It's not working because you can't have expressions as a case "name" or whatever. It must already be a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM