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

Will Feldman
Will Feldman
3,121 Points

Would the following code also work?

var number = 3

if number % 5 == 0 && number % 3 == 0 {
    println("FizzBuzz")
}
else if number % 3 == 0 {
    println("Fizz")
}
else if number % 5 == 0 {
    println("Buzz")
}

And as you change the var "number" it would print the correct result.

That looks pretty good to me.

3 Answers

Franciscus Agnew
Franciscus Agnew
23,097 Points

Hello Will,

The short answer... the code would work, but it may not work the way you intended!

The short solution...

var number = 3

if number % 5 == 0 && number % 3 == 0 {
    println("FizzBuzz")
}
else if number % 5 == 0 {
    println("Fizz")
}
else if number % 3 == 0 {
    println("Buzz")
}
else {
    do something... 
}

Good Luck,

Franciscus

Lukas Muller
Lukas Muller
7,347 Points

Got the same result. Awesome!

John Scully
John Scully
1,018 Points

Your code is identical to where I started and will definitely work if you pass it each individual value. But the real power of code is in the ability to automate so that the computer does all the work for you.

Simply replace your var with "for numbers in 1...50 {"

and you've got it. Just have to close the curly braces at the end of your code.