fizzbuzz - 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?
解决方案
如果您的问题是关于使用的+=
,那么它是一种简短的写作形式output=output+"Fizz"
。因此,我们可以简单地写而不是写上面的output+="Fizz"
推荐阅读
- stm32 - 正确配置 STemWin 以将 FlexColor 驱动程序与 ST7735 显示驱动程序一起使用
- python - 自定义数据生成器
- c# - 在读取 CSV 文件时替换值并保存文件(C#)
- gradle - Gradle 错误:找不到参数的方法 jar()
- ruby-on-rails - 具有 2 深度关联的嵌套资源
- android - 更新到 com.android.tools.build:gradle:3.4.1 -> jRuntimeException: android.databinding.tool.util.LoggedErrorException: 发现数据绑定错误
- amazon-web-services - 在 AWS lambda 中生成唯一的 4 位 id
- haskell - 具有新数据的限制类型集,例如“树 a”
- vue.js - Vuetify v-text-field 在更改时不更新值。即使在 $forceUpdate()
- angular - 使用被 Chrome 广告块阻止的 Angular 中的 blob 渲染 PDF