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

Amazing! I used the same idea! I can't believe it. But something went weird on me. However there are zero errors. Why?

let number = 15
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 is not divisible by 3 or 5")
}


// If number is 9 the output is ""Fizz"
// If number is 20 the output is "Buzz
// If number is 15 the output is still "Fizz". Why isn't it "FizzBuzz"?" 
J.D. Sandifer
J.D. Sandifer
18,813 Points

The problem is you're checking if the number is divisible by 3 before checking if it's divisible by both 3 and 5. Either way it stops at the first if statement and numbers like 15 never get to the third check - the second else if statement.

Just swap your third if statement (2nd else if) and the first one and you should be good to go.

Also, you might want to read the directions again to see what your final else statement should be doing...I think it says to do something else.

By the way, it looks like you're like me in that you didn't ever play the FizzBuzz game. I didn't use a for loop either...because I didn't know that's how the game worked. (And it doesn't explain it in the directions.) I'm glad to see I'm not the only one!

J.D. It's the first time I've heard about the FizzBuzz game lol I didn't use a loop because according to the instructions it actually doesn't need a loop to present the logic. He just asks to show what happens for three conditions and that's what I did. In the prior video prior he didn't use a loop either. He just manually typed the Int value so that's what I did. He typed "var distance = 120 // in miles" and kept changing the distance manually so I just did the same. I just coded it thinking a kid was inputting the numbers themselves. Btw, I placed "if number % 3 == 0 && number % 5 == 0" at the top and it works perfect now even without the loop. Using a loop would've been better though to put my knowledge to the test. If you're around the same level maybe we can help each other out sometimes. Peace!!!!!

1 Answer

J.D. Sandifer
J.D. Sandifer
18,813 Points

The problem is you're checking if the number is divisible by 3 before checking if it's divisible by both 3 and 5. Either way it stops at the first if statement and numbers like 15 never get to the third check - the second else if statement.

Just swap your third if statement (2nd else if) and the first one and you should be good to go.

Also, you might want to read the directions again to see what your final else statement should be doing...I think it says to do something else.

By the way, it looks like you're like me in that you didn't ever play the FizzBuzz game. I didn't use a for loop either...because I didn't know that's how the game worked. (And it doesn't explain it in the directions.) I'm glad to see I'm not the only one!