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

Joseph F Fares Jr
Joseph F Fares Jr
517 Points

FizzBuzz Question

Hi, the instructor used a range of numbers to go thru to solve this programming challenge.

The way that I have chosen seems to not be working and I am unsure why? Would it be because I have not defined a range of #'s? Am I missing a piece of If else statement?

Thank you!

let Fizz = 3.0
let Buzz = 5.0
var variableNumber = 15.0

If variableNumber % Fizz == 0 {

 else if variableNumber % Buzz == 0 {
    print ("Buzz")

 else if variableNumber % Fizz == 0 && variableNumber % Buzz == 0 {
   print ("FizzBuzz")

else {
    print ("")


2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Joseph F Fares Jr,

There's a few things we need to consider here. First theres a minor typo, your first if statement starts with a capital I. The code will not work if it is capitalized.

Second, your variable names should always be in lowerCamelCase. UpperCamelCase format is used to denote Types in Swift.

Third, you actually want to evaluate for the case where the variableNumber is a multiple of both 3 and 5 in your first if case. The reason for this is because the way you have your code set up now, your last else if block will never be executed. This is because if a number is both a multiple of 3 and 5, it would've already evaluated as true in one of the preceding blocks. As soon as your code matches a condition, it jumps out of the block and continues after the very last curly brace of your if/else chain.

let fizz = 3.0
let buzz = 5.0
var variableNumber = 45.0

if (variableNumber % fizz == 0) && (variableNumber % buzz == 0) {
} else if variableNumber % fizz == 0 {
    print ("Fiz")
} else if variableNumber % buzz == 0 {
} else {
    print("Neither fizz nor buzz")

Good Luck

Joseph F Fares Jr
Joseph F Fares Jr
517 Points

Thank you so much Steven. Now it makes so much more sense the way you have it now!