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 trialAlia Khan
3,733 PointsSwift FizzBuzz Challenge
Hi I completed the challenge in playground and there are no errors however when I paste that into the compiler it won't accept my answer. I have made the changes and changed the number variable to n like it asked but I'm a bit confused as to what it wants me to do with the last bit. Ive gotten rid of the last else statement and it still won't compile.
This is my code
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
var n = 10
if (n % 3 == 0 && n % 5 == 0) {
print("FizzBuzz") } else if (n % 3 == 0) {
print("Fizz")
} else if (n % 5 == 0) {
print("Buzz") } else { (n % 3 != 0 && n % 5 != 0)
print("\(n)")
}
// End code
return "\(n)"
}
1 Answer
miikis
44,957 PointsHi Alia,
The Challenge says to use return statements instead of the print() function. Your code should look something like this:
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
if (n % 3 == 0 && n % 5 == 0) {
return "FizzBuzz"
}
if (n % 3 == 0) {
return "Fizz"
}
if (n % 5 == 0) {
return "Buzz"
}
// End code
return "\(n)"
}
Alia Khan
3,733 PointsAlia Khan
3,733 PointsHi what about this line (n % 3 != 0 && n % 5 != 0) there was no error in playground when I wrote this, is is wrong
Thanks for your help :)
miikis
44,957 Pointsmiikis
44,957 PointsYeah, that's wrong.
The modulo operator (%) is equal to the remainder of a division. And if the remainder of a division is zero, then that means that the second number of that division goes into the first number evenly. Which is just another way of saying that the second number is a multiple of the first, right? So that's what we're looking for in the FizzBuzz problem: I give you an arbitrary number and you tell me if that number is a multiple of 3, a multiple of 5 or a multiple of both 3 and 5. In other words: When does n % 3 = 0? When does n % 5 = 0? And when does n % 3 = 0 and n % 5 = 0?
Does that answer your question?
finndieckhoff2
1,547 Pointsfinndieckhoff2
1,547 PointsIn my case, they never managed the return statement in the videos before this challenge. How should I know about it ? Works return in the same way as print()?