JavaScript Algorithm: FizzBuzz javascript fizzbuzz问题
We are going to write a function called fizzbuzz
that will accept no arguments.
The goal of this function is to print out all numbers from 1 to 100 but with three exceptions:
- For every number that is divisible by 3 and 5, console log
"FizzBuzz"
. - For every number that is divisible by only 3 and not 5, console log
"Fizz"
. - For every number that is divisible by only 5 and not 3, console .log
"Buzz"
.
Here is a partial example with numbers from 12 to 20:
...
"Fizz"
13
14
"FizzBuzz"
16
17
"Fizz"
19
"Buzz"
...
First, we create a for-loop that will count from 1 to 100.
for (let i = 1; i <= 100; i++) {}
Inside the for-loop, we will create a series of if...else statements. The first if-statement checks to see if the number is divisible by both 3 and 5. If that’s the case, we print "FizzBuzz"
using console.log()
.
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
}
Next, we create an else if statement. If the number doesn’t divide by both 3 and 5 but it is divisible by 3 only, we print out "Fizz"
.
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
}
If a number doesn’t equally divide by both 3 and 5 and it doesn’t equally divide by only 3, then we check to see if the number is divisible by 5 only. If that is the case, we print out "Buzz"
.
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
}
Finally, if the number doesn’t divide equally by 3 and/or 5, print out the number itself.
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
That concludes our fizzbuzz
function. This algorithm happens to be a common interview question. Employers use this to weed out a lot of programmer candidates. You’ll see all sorts of creative ways to solve this problem especially the ones that have an emphasis on succinctness. My solution isn’t about that. This is the most basic and generic example of FizzBuzz you’ll see and plus, it’s easier to read.
Here is the rest of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function fizzbuzz(){ for ( let i = 1; i <= 100; i++){ if (i % 3 === 0 && i % 5 === 0){ console.log( "FizzBuzz" ); } else if (i % 3 === 0){ console.log( "Fizz" ); } else if (i % 5 === 0){ console.log( "Buzz" ); } else { console.log(i); } } } |
while loop method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | ar output = []; var count = 1; function fizzbuzz() { // write code here - 1-100 numbers divisible by 3 named fizz and by 5 name buzz, and by both then name fizzbuzz. // type fizzbuzz(); in console to make work while (count <= 100) { if (count % 3 ==0 && count % 5 ===0) { output.push( "FizzBuzz" ); } else if (count % 3 ===0) { output.push( "Fizz" ); } else if (count % 5 ===0) { output.push( "Buzz" ); } else { output.push(count); } count++; } console.log(output); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端