0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FizzBuzz問題〜初心者バージョン

Posted at

#1. 課題
FizzBuzz問題
1~30までの数字に対し、
数字が3の倍数のとき: Fizz
数字が5の倍数のとき: Buzz
数字が15の倍数のとき: FizzBuzz
上記以外の場合: 数字
と表示

app.js
for(let i = 1; i < 31; i++) {
    if (i % 15 == 0) { //数字が15の倍数のとき
        console.log(`FizzBuzz`); //FizzBuzz表示
    } else if (i % 3 == 5) { //数字が5の倍数のとき
        console.log(`Buzz`); //Buzz表示
    } else if (i % 3 == 0) { 数字が3の倍数のとき
        console.log(`Fizz`); //Fizz表示
    } else {
        console.log(i); //数字をそのまま表示
    }
}

#2.結果

tomoko@MB FizzBuzz % node app.js
1
2
Fizz
4
5
Fizz
7
8
Fizz
10
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
20
Fizz
22
23
Fizz
25
26
Fizz
28
29
FizzBuzz

#3.もっとスマートな書き方をたくさん見つけた
(後日)

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?