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 trial

iOS Swift Basics (retired) Control Flow Exercise: FizzBuzz Generator

Is my solution as viable as Amit's?

var number = 30


if number % 3 == 0 && number % 5 == 0 {
    println("FizzBuzz")
} else if number % 5 == 0 {
    println("Buzz")
} else if number % 3 == 0 {
    println("Fizz")
} else {
    println("No fizzing, buzzing, or fizzbuzzing for you!")
}

My solution works fine, but I was wondering if anyone saw any logical flaws in it since it's using only an if/else statement rather than having it wrapped in a for loop like Amit's solution.

Is it pretty much dealer's choice when it comes to this solution and how to reach it?

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Ryan Ferguson

Is my solution as viable as Amit's?

Yes, I don't see any logical flaw in your code; and frankly, what you've done here, the use of if/else control flow is identical to what Amit showed during the video. Only different that you are checking BuzzFuzz against a single variable, whereas Amit is doing it for a series of variables, hence the use of for loop. There's no right or wrong here, which version to use depends on the situation and what problem your code is trying to solve.