0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FizzBuzzを制す者、基礎を制す!

Last updated at Posted at 2025-02-23

「FizzBuzz? 簡単そう」と思った僕。でも、いざ書くと if の順番 でハマった!間違えると FizzBuzzがFizzになる という罠…。「なぜ?」を考えながら、正しい書き方を整理してみた。
条件の順番が大事!

FizzBuzzの基本ルール:

3の倍数 → Fizz
5の倍数 → Buzz
3と5の倍数 → FizzBuzz(これを最優先に!)

NG例(FizzBuzzが出ない):

if (count % 3 === 0) { 
    console.log("Fizz"); 
} else if (count % 5 === 0) { 
    console.log("Buzz"); 
} else if (count % 3 === 0 && count % 5 === 0) {  
    console.log("FizzBuzz"); }  

☠ if の順番ミス!3の倍数でFizzが出てしまい、FizzBuzzになるはずの数がFizzに…

僕の失敗談と解決話

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?