Instructions
Create a function fizzbuzz that takes a number and returns:
- "FizzBuzz" if the number is divisible by both 3 and 5
- "Fizz" if the number is divisible by 3
- "Buzz" if the number is divisible by 5
- The number as a string otherwise
Hints:
- Check divisibility by both 3 and 5 first
- Use n % 3 == 0 to check divisibility by 3
- Use .to_s to convert number to string
Your Code
def fizzbuzz(n)
if n % 15 == 0
"FizzBuzz"
elsif n % 3 == 0
"Fizz"
elsif n % 5 == 0
"Buzz"
else
n.to_s
end
end
RSpec.describe "fizzbuzz" do
it "returns Fizz for 3" do
expect(fizzbuzz(3)).to eq("Fizz")
end
it "returns Buzz for 5" do
expect(fizzbuzz(5)).to eq("Buzz")
end
it "returns FizzBuzz for 15" do
expect(fizzbuzz(15)).to eq("FizzBuzz")
end
it "returns the number as string for 7" do
expect(fizzbuzz(7)).to eq("7")
end
it "returns FizzBuzz for 30" do
expect(fizzbuzz(30)).to eq("FizzBuzz")
end
end
Results
Click "Run Tests" to see results