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 trialDeneen Edwards
5,626 PointsFizz Buzz Question
Pasan,
At 1st, I coded Fizz Buzz like your provided solution. Then I realized, if I used else if, I would drop out when the number is divisible by 15, 3, and 5. For instance 15, 15 is divisible by 3, 5, and 15. Therefore Fizz, Buzz, and FizzBuzz should be printed.
Then I changed the code to all if statements, so that the number would be tested against each condition every time. Of course, this solution didn't pass.
I re-listened to the video and it doesn't say drop out once a test is met.
Has anyone else found the instructions confusing?
2 Answers
Maria Angelica Dadalt
6,197 PointsThere's nothing wrong with your sintax. The first thing you have to check is for FizzBuzz, that is, if the number is divisible by 3 and 5, then you check only 3 and 5. Otherwise the flow stops at the first and second check, never checking for FizzBuzz numbers. If my answer made any sense, I hope I could help. There is a video with the answer to this challenge though.
Andreas Thorsen
Courses Plus Student 2,925 PointsThis is what I did and it works every time: I hope this helps.
var randomNumber = 16
let fizz = randomNumber % 3 == 0
let buzz = randomNumber % 5 == 0
let fizzBuzz = fizz && buzz
if fizzBuzz {
print("FizzBuzz")
} else if fizz {
print("Fizz!")
} else if buzz{
print("Buzz!")
} else {
print(randomNumber)
}