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 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,867 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.