Created
May 18, 2017 21:43
-
-
Save matt297/28e63eb366a9a311595fe599b204361e to your computer and use it in GitHub Desktop.
Lighthouse Labs - Intro to Web Dev - W2D2 - May 2017 Cohort
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
# The initial solution looked like this: | |
(1..100).each do |x| | |
puts x | |
if x % 3 == 0 && x % 5 == 0 | |
puts "FizzBuzz" | |
elsif x % 3 == 0 | |
puts "Fizz" | |
elsif x % 5 == 0 | |
puts "Buzz" | |
end | |
end | |
# Then, after some refining, it looked like: | |
(1..100).each do |x| | |
div_by_three = (x % 3 == 0) | |
div_by_five = (x % 5 == 0) | |
if div_by_three && div_by_five | |
puts "FizzBuzz" | |
elsif div_by_three | |
puts "Fizz" | |
elsif div_by_five | |
puts "Buzz" | |
end | |
end | |
# And finally, after even more refining: | |
(1..100).each do |x| | |
div_by_three = (x % 3 == 0) | |
div_by_five = (x % 5 == 0) | |
output = "" | |
if div_by_three | |
output += "Fizz" | |
end | |
if div_by_five | |
output += "Buzz" | |
end | |
if output.length > 0 | |
puts output | |
end | |
end | |
# This is beyond what was covered in class, but it turns out you can also write if | |
# statements at the end of a line, and that line won't get run unless the if statement | |
# evaluates to true. So, we could actually take our code one step further: | |
(1..100).each do |x| | |
div_by_three = (x % 3 == 0) | |
div_by_five = (x % 5 == 0) | |
output = "" | |
output += "Fizz" if div_by_three | |
output += "Buzz" if div_by_five | |
puts output if output.length > 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment