Created
September 1, 2011 08:58
-
-
Save samrat/1185748 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
tf = 0 | |
t = 1 | |
(1..100).each do |num| | |
if num % 3 == 0 and num % 5 == 0 | |
tf += 1 | |
else if num % 3 == 0 and num % 5 != 0 | |
t += 1 | |
end | |
end | |
end | |
puts tf, t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some weird reason there was no comment form on your reddit question.
First, this doesn't appear to be a correct implementation of fizzbuzz. Why aren't you printing fizz or buzz or fizzbuzz anywhere?
Second, Ruby's "else if" is actually "elsif", "else if" is a syntax error.
Third, you do, indeed, have to many "ends". There should just be one to close the if block.
Fourth, some logic advice: since you're taking care of the specific case in if by seeing if both 3 and 5 are multiples of num, you don't need to check that 5 is not a multiple in the elsif expression.
Fifth, you're missing the last part of the fizzbuzz program. You've taken care of the situation where both 3 and 5 are multiples of num so you'd print "fizzbuzz", then you took care of just 3 being a multiple, so you print "fizz". To complete this you need to print "buzz" if just 5 is a multiple of num, and finally if no case matches just print num.
On indentation: the closing "end" should match it's opening caller. So your last "end" should be on the same indentation level as the beginning of (1..100).each ... and first nested "end" should be on the same level as the opening "if". The "elsif" doesn't have an "end", it's part of the "if" block.