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 2.0 Collections and Control Flow Control Flow With Conditional Statements Solution to FizzBuzz

Kevin Bullis
Kevin Bullis
13,770 Points

What if we actually use random numbers, as the video asks for?

In the video, Pasan sets up the question saying, "given a random number," so my solution uses a random number, not the range 1 to 100.

Does the following answer the challenge, for random numbers up to 150?

var randomNum = Int (arc4random_uniform(UInt32(150)))

switch 0 {
case randomNum: print(0)
case randomNum % 3 + randomNum % 5: print("FizzBuzz")
case randomNum % 5: print("Buzz")
case randomNum % 3: print("Fizz")
default: print(randomNum)
}
Carlos Henrique Martins
Carlos Henrique Martins
Courses Plus Student 1,483 Points

Good work! It will work also.

You will be generating random numbers every time you compile your code. Just a observation as well,I am putting a cleaner code right here:

var randomNum = Int (arc4random_uniform(UInt32(150)))
switch 0 {
    case randomNum:
        print(0)

    case randomNum % 3 + randomNum % 5:
        print("FizzBuzz")

    case randomNum % 5:
        print("Buzz")

    case randomNum % 3: print("Fizz")

    default:
        print(randomNum)
}
Kevin Bullis
Kevin Bullis
13,770 Points

What did you do to get your code to show up in that black background?

5 Answers

Carlos Henrique Martins
PLUS
Carlos Henrique Martins
Courses Plus Student 1,483 Points

It's called "Markdown Cheatsheet". If you want to write a code in this text filed (the box for answer or reply text), you put ()Swift print("Hello") ()

I've parenthesis around the three symbols because it will activate the markdown cheatsheet, but if you want to activate this black background, just need to take them off.

Kevin Bullis
Kevin Bullis
13,770 Points

testing.

print(hello)

Hello, this challenge is a bit confusing. I think by random number he means passing any number that comes to the user's mind when calling the function and there is no need for for loop there.

Your approach looks a creative one. Here is my solution:

var rand = Int (arc4random_uniform(UInt32(150)))

switch (rand % 3 , rand % 5) {
case (0,0) : print("fizzBuzz \(rand)")
case (0, _): print("Fiz \(rand)")
case (_ , 0): print("Buzz \(rand)")
default : print(rand)

}

what is the meaning of 0 ?