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

Why isn't this working for the Fizz challenge?

It says to write a code that will print out the word "Fizz" whenever a number is divisible by three in the set. So I wrote:

let numbers = [1,2,3,4,5,6]
for number in numbers {
if ("\(number)%3=0{println    ("Fizz")})
}
var item = 6
item

1 Answer

Be careful when opening and closing with Parentheses and Quotes. You're very close to the answer, but you need to use the == operator instead of = operator.

let numbers = [1,2,3,4,5,6]
for number in numbers {
    if number % 3 == 0 {
        println("Fizz")
    }
}

It worked!! Thanks Sean! Can you explain the difference between when you use "(number)" and when you do not?

Oh I see, what you're trying to do. You're trying to use String Interpolation in an if statement. String Interpolation is meant readability in code for Output or println(). It's best used in println() or between quotes that you want 2 or more variables in a sentence.

// Example #1 w/o String Interpolation
var greeting = "Hello"
var name = "Sean"
println(greeting + " " + name) // Output: "Hello Sean"

// Example #2 w/ String Interpolation
var greeting = "Hello "
var name = "Sean"
println("\(greeting) \(name)") // Output: "Hello Sean"

For if statement, you can use the variable without String Interpolation, because it's asking a condition not a String type. Hopefully, this makes sense. I'm sure, a Moderator can do better explanation at this.

Happy Coding!

Wonderful, got it. Thank you!