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 trialRoger Soles
1,584 PointsI am using switch case here, and using remainder functions in each case. Can't seem to figure out where I am going wrong
I cannot seem to get the hang of this one. Xcode is giving me an error for each of the 3 cases - " Expression pattern of type 'Bool' cannot match values of type 'Int'."
Am I not able to use the remainder operator in the cases? Or am I not formatting it correctly? Something else entirely?
my code formatted for the challenge is below but in Xcode I was using the print function and defined n as a range.
Any help would be appreciated! Thanks.
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
switch n {
case n % 5 == 0: return "Buzz"
case n % 3 == 0: return "Fizz"
case n % 5 == 0 && n % 3 == 0: return "FizzBuzz"
}
// End code
return "\(n)"
}
3 Answers
jonlunsford
15,480 PointsGet rid of everything below //end code and try again. Also remember that a default value is required on switch statements. After "FizzBuzz" you need:
default: return "\(n)"
Lastly, you might want to move the last case statement up and make it the first case statement.
Robert Baghai
5,492 PointsAs jonlunsford stated, when working with switch case always assign it a default value after your cases. The reason why your code won't work is because you are not handling all conditions that may happen, meaning what happens if the number does not have a remainder of 0 when divided by 3, or 5, or 3 && 5. You might think, well we don't care about those, but the computer sure does. It has no idea what to do if the value of the integer does not meet the three conditions you assigned it. Hence, the default statement that the computer can rely on when it does not know 'what to do'.
Roger Soles
1,584 PointsThanks Robert, the challenge stated that the default case should be handled by the code already set up here, presumably the -- return "(n)" -- bit.
jonlunsford
15,480 PointsYou could have used an IF block as well instead of the Switch. Glad you got through it though.
Roger Soles
1,584 PointsYeah thats what the solution said, but it seems weird that they would cover off switch, and move into a challenge on the tail of it that doesnt cover it. Anyways, now I know, and I learned something else in the process.
Thanks.
Roger Soles
1,584 PointsRoger Soles
1,584 PointsThanks Jon, I think it was actually the order that was affecting it. I was able to complete this challenge using a "where" statement. But I had to look that up elsewhere, and it has not been covered yet. Strange!
Thanks for the help!