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.

javascript を使ったFizzBuzz

Posted at
<!doctype html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>fizzbuzz</title>
    </head>
    <body>
        <script>
            for (var i = 1; i < 101; i++) {
                if (i % 3 === 0 && i % 5 === 0) {
                    document.write("FizzBuzz" + "<br/>");
                } else if (i % 3 === 0) {
                    document.write("Fizz" + "<br/>");
                } else if (i % 5 === 0) {
                    document.write("Buzz" + "<br/>");
                } else {
                    document.write(i + "<br/>");
                }
            }
        </script>
    </body>
</html>

もし3と5で割り切れるかどうかの条件を最後に持ってきた場合、たとえば15なら先に5のみで割り切れると判定されてbuzzが表示されてしまう。(この問題に初めて取り掛かった頃はこれに気づかなかった)
あと、一つづつ改行するやり方も検索しても何故かなかなかヒットせず、困った記憶がある。
そんなことで困りたくないですね。

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?