首页 > 解决方案 > What is going on in this solution to the Fizzbuzz question?

问题描述

      for (let number = 0; number <= 100; number++ ) {
            let output = ""
            if (number % 3 == 0) output += "Fizz"
            if (number % 5 == 0) output += "buzz"
            console.log(output || number)
  
            }

I understand why it finds the modulo for 3 and 5. But why does this also find the modulo for 15? (The question asks to also find the numbers that are divisible by 3 and 5 and print "Fizzbuzz").

I was able to solve the question in a different manner and was offered this as one of the ideal solutions.

Is the += after the output somehow multiplying the other two remainders?

标签: fizzbuzz

解决方案


如果您的问题是关于使用的+=,那么它是一种简短的写作形式output=output+"Fizz"。因此,我们可以简单地写而不是写上面的output+="Fizz"


推荐阅读