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 trialMichael Harris
1,528 PointsMy code works perfectly in the playground. It's not compiling in your editor. I have replaced my print().
My code:
var n = 31 let fizz = "Fizz" let buzz = "Buzz"
if n % 3 == 0 && n % 5 == 0 { print("(fizz)" + "(buzz)") } else if n % 5 == 0 { print(buzz) } else if n % 3 == 0 { print(fizz) } else { print(n) }
I have removed all my variables/constants, added return "FizzBuzz". Still not compiling.
func fizzBuzz(n: Int) -> String {
if n % 3 == 0 && n % 5 == 0 {
return "FizzBuzz"
} else if n % 5 == 0 {
return "FizzBuzz"
} else if n % 3 == 0 {
return "FizzBuzz"
} else {
return "FizzBuzz"
}
return "\(n)"
}
1 Answer
Martin Wildfeuer
Courses Plus Student 11,071 PointsThe assignment asks you to return "FizzBuzz", "Buzz" or "Fizz" as a String, instead of printing it to the console. "FizzBuzz" is just one example of a return statement.
Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz".
Therefore, the following should work:
func fizzBuzz(n: Int) -> String {
if n % 3 == 0 && n % 5 == 0 {
return "FizzBuzz"
} else if n % 5 == 0 {
return "Buzz"
} else if n % 3 == 0 {
return "Fizz"
} else {
// End code
return "\(n)"
}
}
P.S. Code that compiles in a playground has almost certainly no syntax errors, but that does not guarantee the correct result.
Hope that helps :)
Michael Harris
1,528 PointsMichael Harris
1,528 PointsNevermind. I took the add return "FizzBuzz" to literally. I added return "Buzz", return "Fizz" etc. and it worked.