LoginSignup
0
0

More than 1 year has passed since last update.

WEB開発をやり始めてみる(Ruby:FizzBuzz)

Last updated at Posted at 2022-10-16

やったこと

FizzBuzz

def fizz_buzz(n)
    if n % 15 == 0
        "FizzBuzz"
    elsis n % 3 == 0
        "Fizz"
    elsis n % 5 == 0
        "Buzz"
    else
        n.to_s
    end
end

puts(1)
puts(2)
puts(3)
puts(5)
puts(15)

まとめ

15が先にあるのは、仮に3が上にあると3が該当するため、15を先に入れている。

def fizz_buzz(n)
    if n % 15 == 0
        "FizzBuzz"

n.to_sは「n」に対して数値で返すメソッド(.to_s)

else
        n.to_s
    end
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