Last active
December 18, 2015 02:09
-
-
Save abrahamsangha/5709156 to your computer and use it in GitHub Desktop.
The challenge was to create a method that returned an array of numbers from 1 to 1000 but with each number that included 5 as a factor replaced with "Fizz", each number that included 7 as a factor replaced with "Blam" and each number that included both 5 and 7 as factors replaced with "FizzBlam". The additional challenge was to do it in one line…
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 fizzblam | |
arr = (1..1000).to_a.map { |elem| [("Fizz" if (elem % 5).zero?), ("Buzz" if (elem % 7).zero?), (elem unless ((elem % 5).zero?) || ((elem % 7).zero?))].compact.join } | |
end | |
#another way to do it using string interpolation | |
def fizzblam | |
arr = (1..1000).to_a.map { |elem| "#{'Fizz' if elem % 3 == 0}#{'Buzz' if elem % 5 == 0}#{elem unless elem % 3 == 0 || elem % 5 == 0}" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment