Last active
August 29, 2015 14:21
-
-
Save jaeming/b2b885cf24231370c99f to your computer and use it in GitHub Desktop.
Exploring FizzBuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz | |
(1..100).each do |n| | |
if (n % 3 == 0) && (n % 5 == 0) | |
puts 'FizzBuzz' | |
elsif n % 3 == 0 | |
puts 'Fizz' | |
elsif n % 5 == 0 | |
puts 'Buzz' | |
else | |
puts n | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz | |
(1..100).each do |n| | |
case | |
when (n % 3 == 0) && (n % 5 == 0) | |
puts 'FizzBuzz' | |
when n % 3 == 0 | |
puts 'Fizz' | |
when n % 5 == 0 | |
puts 'Buzz' | |
else | |
puts n | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz | |
(1..100).each do |n| | |
fizz = (n % 3 == 0) | |
buzz = (n % 5 == 0) | |
puts case | |
when fizz && buzz | |
'FizzBuzz' | |
when fizz | |
'Fizz' | |
when buzz | |
'Buzz' | |
else | |
n | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz | |
(1..100).each do |n| | |
fizz = (n % 3 == 0) | |
buzz = (n % 5 == 0) | |
result = 'fizz' if fizz | |
result = 'buzz' if buzz | |
result = 'FizzBuzz' if fizz && buzz | |
result ||= n | |
puts result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizz | |
fizz = 3 | |
buzz = 5 | |
fizzbuzz = 15 | |
(1..100).each do |n| | |
while n == fizz do | |
result = 'fizz' | |
fizz += 3 | |
end | |
while n == buzz do | |
result = 'buzz' | |
buzz += 5 | |
end | |
while n == fizzbuzz do | |
result = 'fizzbuzz' | |
fizzbuzz += 15 | |
end | |
result ||= n | |
puts result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(f, b) | |
(1..100).each do |n| | |
fizz = (n % f == 0) | |
buzz = (n % b == 0) | |
result = "Fizz" if fizz | |
result = "Buzz" if buzz | |
result = "FizzBuzz" if fizz && buzz | |
result ||= n | |
puts result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(options={first_number: 3, second_number: 5, first_name: 'fizz', second_name: 'buzz'}) | |
(1..100).each do |n| | |
condition1 = (n % options[:first_number] == 0) | |
condition2 = (n % options[:second_number] == 0) | |
result = options[:first_name] if condition1 | |
result = options[:second_name] if condition2 | |
result = options[:first_name] + options[:second_name] if condition1 && condition2 | |
result ||= n | |
puts result | |
end | |
end | |
fizzbuzz({first_number: 3, second_number: 7, first_name: 'sizzle', second_name: 'fizzle'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fizz | |
attr_accessor :first_number, :second_number, :first_name, :second_name | |
def initialize(first_number, second_number, first_name, second_name) | |
@first_number = first_number | |
@second_number = second_number | |
@first_name = first_name | |
@second_name = second_name | |
end | |
def buzz | |
(1..100).each do |n| | |
fizz = (n % first_number == 0) | |
buzz = (n % second_number == 0) | |
result = first_name if fizz | |
result = second_name if buzz | |
result = (first_name + second_name) if fizz && buzz | |
result ||= n | |
puts result | |
end | |
end | |
end | |
f = Fizz.new(4, 7, 'Benji', 'Dalton') | |
f.buzz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment