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 Introducing FizzBuzz

john ledesma
john ledesma
6,944 Points

FizzBuzz question.

new to swift Hello all I've been stuck on the FizzBuzz question, I've known from the beginning that I could either use if statment or switch and after trying many different things and piecing other examples from the web I got

func fizzbuzz(n: Int) -> String {
    switch (n % 3 == 0, n % 5 == 0) {
    case (true, false):
        return "Fizz"
    case (false, true):
        return "Buzz"
    case (true, true):
        return "FizzBuzz"
    default:
        return String(n)
    }
}

However this works in the playground but when submitting as my answer it says its incorrect. If anyone can help it will be greatly appreciated :)

Nathan Tallack
Nathan Tallack
22,160 Points

Formatted Johns code to make it easier to read.

func fizzbuzz(n: Int) -> String {
    switch (n % 3 == 0, n % 5 == 0) {
        case (true, false): return "Fizz" 
        case (false, true): return "Buzz" 
        case (true, true): return "FizzBuzz" 
        default: return String(n)
    }
}
Edwin Garcia
seal-mask
.a{fill-rule:evenodd;}techdegree
Edwin Garcia
iOS Development Techdegree Student 4,108 Points

Your code for the switch statement is correct there is just one little problem with your function. The "b" in your function fizzbuzz is not CAPITALIZED that is why your code probably did not work. This is late but I wanted to figure out why your code didn't work :)

4 Answers

jonlunsford
jonlunsford
15,480 Points

John, Did you try implementing this without the function code - just as the switch statement? If that doesn't work it could be related to the way you set up the Switch statement on tuples (very creative by the way...).

john ledesma
john ledesma
6,944 Points

jonlunsford thank you after doing it without the function code it worked. I really apprcieate your help :)

I think they are looking for you to use if/in statement to iterate through a given range, instead of creating a function.

Here is an example

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

(I can't find the original question so I might be totally off-base...)

john ledesma
john ledesma
6,944 Points

After listening to jonlunsford advice i was able to complete the challenge, I believe your answer is correct also. thank you for your help was struggling on this question :)

Hi, I tried this one in playground and worked but couldn't submit it in to challenge form. any ideas?

func fizzbuzz( n: Int) -> String {

switch n {
case _ where n % 3 == 0 && n % 5 == 0 : return "FizzBuzz"
case _ where n % 3 == 0 : return "Fizz"
case _ where n % 5 == 0 : return "Buzz"

default: return String(n)
}

return " \(n)" //Will never be executed (xcode warning)

}

fizzbuzz(15) //FizzBuzz

Any help with Rzgar's question? I am having same issue