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 2.0 Collections and Control Flow Control Flow With Conditional Statements FizzBuzz

Frank Duah
Frank Duah
28,678 Points

Can someone please show the answer for Swift Basics Control Flow FizzBuzz. directions /code in the editor are confusion

The code in the editor and the instructions are throwing me off.

2 Answers

Hello Frank,

Rather than just throw you the answer, let me guide you thru it:

One of the possible solutions is to use a simple IF statement. Since the IF statement executes until one condition is true, make sure to start with FizzBuzz (a number divisible by 3 and 5):

    if fizzNumber % 3 == 0 && fizzNumber % 5 == 0 {
        print("FizzBuzz")
    } else if fizzNumber % 3 == 0 {
        print("Fizz")
    } else if fizzNumber % 5 == 0 {
        print("Buzz")
    } else {
        print("Is not a FizzBuzz number")
    }

Now I understand the test directions are confusing, it seems the only current way for Treehouse to check your answer is by altering your syntax. Basically replaced "print" with "return" and "fizzNumber" with "n". As removed the default (else if) statement. This is confusing, but it's the only way they can actually validate your code.

     if n % 3 == 0 && n % 5 == 0 {
        return("FizzBuzz")
    } else if n % 3 == 0 {
        return("Fizz")
    } else if n % 5 == 0 {
        return("Buzz")
    }
Frank Duah
Frank Duah
28,678 Points

Thank you very much @mfagency . I thought I understood the question but I was unable to confirm the answer. I realize my return statements were incorrect. I did not have parenthesis when returning my strings. I must of been assuming the return statement would be similar to javascript javascript since we did not see one during this entire course. The instructions even display the return statement without the brackets lol.