Skip to content

Instantly share code, notes, and snippets.

@kvirani
Last active September 12, 2017 09:54
Show Gist options
  • Save kvirani/b7f3674d6a3b5572471c to your computer and use it in GitHub Desktop.
Save kvirani/b7f3674d6a3b5572471c to your computer and use it in GitHub Desktop.
Fizzbuzz

You should be familiar with this code, but go ahead and execute it to make sure it’s working as expected.

Let’s now make our fizzbuzz more robust and flexible.

Task 1

Refactor (improve) this code by implementing a method (called fizzbuzz) that takes in the starting and ending number so that we can programmatically change which numbers our fizzbuzz starts and ends at, instead of the usual 1 and 100.

Task 2

Think about other methods that can be implemented to further refactor this code and make it more readable. Remember that readability does not necessarily mean fewer lines of code.

1.upto(100) do |i|
if i % 5 == 0 && i % 3 == 0
puts "FizzBuzz"
elsif i % 5 == 0
puts "Buzz"
elsif i % 3 == 0
puts "Fizz"
else
puts i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment