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 FizzBuzz

Antony Gabagul
Antony Gabagul
9,351 Points

Code won't work

Unfortunately my code won't compile, and I can't seem to find a reason. Thanks

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  switch n {
    case (n % 6 == 0) : print("Fizz")
    case (n % 5 == 0) : print("buzz")
    case (n % 6 == 0 && n % 5 == 0) : print("FizzBuzz")
    default: print(n)
}

  // End code
  return "\(n)"
}

3 Answers

Paul Brazell
Paul Brazell
14,371 Points

If/Else statement would be better here. Change your cases to If statements.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Keep in mind that also all print statements need to be changed to return as indicated by the instructions. Also, you're supposed to have it as determining if divisible by 3 and 5. Not 6 and 5.

That being said, the very next video shows you the solution. Happy coding!

James Tench
James Tench
43,891 Points

The reason is because you are switching on the value of n. Therefore you cannot make it part of a conditional statement like you have it in the case statement.

I would try the challenge with an if else if else type of check.

If you want to use a switch I think you need to use the "where" keyword in your case statements.