Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Conditional Statements Introducing FizzBuzz

Deneen Edwards
Deneen Edwards
5,626 Points

Fizz 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
Maria Angelica Dadalt
6,197 Points

There'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
PLUS
Andreas Thorsen
Courses Plus Student 2,925 Points

This 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)

}