Last active
August 29, 2015 14:01
-
-
Save kencrocken/4099d6079c7926a6826d to your computer and use it in GitHub Desktop.
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
# check if 'num' is divisble by either 3 or 5. If true, check if divisible by 3 - | |
# print 'fizz' or if false ''. Same with 5, but print 'buzz'. Came up with the idea when fiddling around | |
# and had "num%3 * num%5 == 0 ? 'fizz' + 'buzz' : num". I believe it works just like concatenating a string | |
# Essentially, I believe that is what is going on, this is merely concatenating strings whichis why the '+' | |
# works to connect the two conditions; i.e., really connecting two strings. | |
def fizzbuzz(num) | |
num % 3 * num % 5 == 0 ? ( num % 3 == 0 ? "fizz" : "" ) + ( num % 5 == 0 ? "buzz" : "" ) : num | |
# num % 3 == 0 && num % 5 == 0 ? "fizzbuzz" : num % 3 == 0 ? "fizz" : num % 5 == 0 ? "buzz" : num | |
end | |
1.upto(100) do |num| | |
puts fizzbuzz(num) | |
end | |
def assert(truthy) | |
raise "There was an error" if !truthy | |
end | |
assert fizzbuzz(1) == 1 | |
assert fizzbuzz(2) == 2 | |
assert fizzbuzz(3) == 'fizz' | |
assert fizzbuzz(4) == 4 | |
assert fizzbuzz(5) == 'buzz' | |
assert fizzbuzz(9) == 'fizz' | |
assert fizzbuzz(15) == 'fizzbuzz' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment