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

why is my code not working?

My code looks similar but it is not working. Here is the screenshot.

import UIKit
var number = 10


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

4 Answers

Perhaps you need to add == 0 on this rule?

number % 3 == 0 &&  number % 5 == 0 {

Yes, I figured that out but I'm still getting an error.

import UIKit var number = 10

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

Loubna Aggoun
Loubna Aggoun
4,052 Points

What does the error say ?

Your code will not work because this condition :

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

should be the first one.

Indeed, if you test with number 6, it will go through the first condition and return Fizz, if you test with number 10 it will go through the second condition and return Buzz. However, if you test with 15 which is a multiple of both 3 and 5, you will only go through the first condition as 15 can be divided by 3. As a consequence, you will never reach the 3rd condition and it will never print "FizzBuzz".

thank you vey much

thank you vey much