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 trialDamian Adams
21,351 PointsFizzBuzz Generator using switch statements solution?
Hi everybody!
In the spirit of curiosity I went about finding different answers to the FizzBuzz generator problem; however, I have yet to think of a way to build one using switch statements. I tried a few variations of this, but my general idea for how the code would look would be something like this:
var numbers = 1...100
for n in numbers {
switch n {
case (n % 3 == 0) %% (n % 5 == 0):
println("FizzBuzz")
case (n % 3 == 0):
println("Fizz")
case (n % 5 == 0):
println("Buzz")
default:
println(n)
}
}
If someone could figure out what the problem in the code is or the right way to do this using switch statements (I suspect improper case statement syntax) I'd very much be in your debt.
Many thanks!
3 Answers
mohitbhatia
Courses Plus Student 5,153 Points <p>
var num = 1...20
for number in num {
switch number{
case number where number % 3 == 0 && number % 5 == 0 :
println("FIzzBuzz")
case number where number % 3 == 0:
println("Fizz")
case number where number % 5 == 0:
println("Buzz")
default:
println(number)
}
}</p>
```
Michael Mahler
8,560 PointsIt does look like you're using improper switch statement syntax. See the docs here.
Emmanuel Ouzan
4,107 PointsHi Damian !
I think that the use of "Switch" for this exercise is a great!
Your Idea is good, but your syntax dose not follow the syntax rules of "Switch" !
I can tell you how to correct your syntax right away, but I will prefer that you will go back to Amit's video talking about "Switch" and find out yourself what is your syntax problem ! :)
In the future, When you will have the same issue with other swift functions and commands, you should always go to the docs of swift and try to understand what is the right syntax for the command that you have used and why your syntax doesn't work ! :)
(IOS Developer library : https://developer.apple.com/library/ios/navigation/ )
Good luck !
Emmanuel.
patrtoikua
1,871 Pointspatrtoikua
1,871 PointsI tried this but on number 15 it's not showing "FizzBuzz" instead it's "Fizz"
patrtoikua
1,871 Pointspatrtoikua
1,871 PointsNevermind. Haha. I found the problem. Thanks.