Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGiulio Autelitano
554 PointsWhy isn't this working?
var number = 15
var three = (number)%3
var five = (number)%5
if five == 0 && three == 3 { println("BuzzFizz") } else if five == 0 { println("Buzz") } else if three == 0 { println("Fizz") }
3 Answers
umerrathore
2,558 PointsThe problem is with this statement:
"if five == 0 && three == 3"
It should be:
"if five == 0 && three == 0"
as your variable three evaluates whether there is any remainder when dividing by the number 3. For the number 15, there would be no remainder for dividing by 5 or by 3, so you 'three' variable would be 0. As such, the way you have written it, the first If condition is never met when number = 15.
I hope this helps!
Srinivasan Senthil
2,266 Pointshahaha.. this was a good one... people have explained the answer above. You need to understand that the moment the elseif statement is executed, the statement becomes true, so the code never jumps to the next line.
var number = 15
var three = (number)%3 three
var five = (number)%5 five
if five == 0 && three == 3 { println("BuzzFizz") } else if five == 0 { println("Buzz") } else if three == 0 { println("Fizz") }
john dev.
Courses Plus Student 1,935 PointsYes, you are right!
john dev.
Courses Plus Student 1,935 PointsHello Giulio Autelitano. I am "sorry" if I misunderstood you. Here is my solution for your problem. I hope it helps you! : )
var three = 3 // choose a number
var five = 0 // choose a number
if five == 0 && three == 3 {
println("BuzzFizz") }
else if five == 0 {
println("Buzz") }
else if three == 0 {
println("Fizz") }
john