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 trialjohn ledesma
6,944 PointsFizzBuzz 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 :)
Edwin Garcia
iOS Development Techdegree Student 4,108 PointsYour 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
15,480 PointsJohn, 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
6,944 Pointsjonlunsford thank you after doing it without the function code it worked. I really apprcieate your help :)
warp42
3,171 PointsI 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)") }}
warp42
3,171 Points(I can't find the original question so I might be totally off-base...)
john ledesma
6,944 PointsAfter 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 :)
Rzgar Espo
872 PointsHi, 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
jacob paul
1,275 PointsAny help with Rzgar's question? I am having same issue
Nathan Tallack
22,160 PointsNathan Tallack
22,160 PointsFormatted Johns code to make it easier to read.