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

Using everything i learned so far, i have to create a program FizzBuzz generator, but i cant, no clue why.please help

instructions

Exercise:

Your challenge is to program a FizzBuzz generator in the Xcode Playground. If a number is divisible by 3 then you print out "Fizz". If it is divisible by 5 then you print out "Buzz". Finally, if it is divisible by both 3 and 5 then you print out "FizzBuzz". This challenge is great because you can apply everything you have learned in this course.

this is what i got...

var numbers = 1...100

for number in numbers { switch number { case ( number % 3 == 0 ): println("Fizz") case ( number % 5 == 0 ): println("Buzz") case ( number % 3 == 0, number % 5 == 0 ): println("FizzBuzz") default: println(number) } }

// it says type "Bool" does not conform to protocol "IntervalType"

how many things am i doing wrong. no idea how to do this. please help.

ps. Im really new and trying to start from scratch

4 Answers

Try this, Abdurahman:

for number in 1...100
{
    if number % 3 == 0 && number % 5 == 0
    {
        println("FizzBuzz")
    }
    else if number % 5 == 0
    {
        println("Fizz")
    }
    else if number % 3 == 0
    {
        println("Buzz")
    }
    else
    {
        println(number)
    }
}

The reason

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

has to be evaluated first is because if it were evaluated at the end of the IF statement, it would never run. Why? Since the number has to be divisible by both 3 and 5, if EITHER of those conditions are met, the if statement would print either "Buzz" or "Fizz". BOTH conditions have to be true for "FizzBuzz" to be printed.

switch statement really isn't a good fit for this problem, try approach this problem with if else instead.

oh wow your right!!. thanks will Now this is what i got. its almost working i think, but now i dont know how to write the command on how to get it divisible by both 3 and 5 and make it write "FizzBuzz"

this is what i have...

var numbers = 1...100

for number in numbers { if number % 3 == 0 { println("Fizz") } else if number % 5 == 0 { println("Buzz") } else if number % 3 , 5 == 0 { println("FizzBuzz")} else { println(numbers) } }

how do i fix this section because i know its wrong.

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

it worked zachary!!!.thanks so much

You're welcome, Abdurahman! Keep up the good work!