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

Andrew Chung
Andrew Chung
5,203 Points

Code not working

for i in 1...20 {
    if (i % 3 == 0) && (i % 5 ==0) {
        println("Fizzbuzz")
    } else if (i % 3 == 0) {
        println("Fizz")
    } else if (i % 5 == 0) {
        println("Buzz")
    } else {
        println(i)
    }
}

From what I can tell my code is identical to the one in the video but xcode is saying that I need a comma after the 5 in the second line.

You need to simply add a space between the == and 0 in your second line.

for i in 1...20 {
    if (i % 3 == 0) && (i % 5 == 0) {
        print("Fizzbuzz")
    } else if (i % 3 == 0) {
        print("Fizz")
    } else if (i % 5 == 0) {
        print("Buzz")
    } else {
        print(i)
    }
}
Andrew Chung
Andrew Chung
5,203 Points

Thanks Kyle! I didn't think the spacing mattered but I guess it does in Swift.

1 Answer

Tingting Gu
Tingting Gu
10,760 Points

Kyle's answer works. You can also do it in conditions without parentheses.