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 trialZhenia Ps
iOS Development Techdegree Student 407 PointsFizzBuzz
Hey guys. Not sure why it is not working here with func. I have tested my code in Playground and it was compiled just fine.
Any help is much appreciated.
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
for n in 1...100 {
if n % 3 == 0 && n % 5 == 0 {
print("FizzBuzz")
} else if n % 5 == 0 {
print("Buzz")
} else if n % 3 == 0 {
print("Fizz")
}
}
// End code
return "\(n)"
}
Alexander Davison
65,469 PointsThe print
function just prints some value onto the screen and the return
keyword is used for returning values.
Even built-in functions like print
actually have a return value. The print
function simply returns nil
(basically a Swift way of saying "nothing").
Meanwhile, it would be nice to click the Best Answer button located under an answer that has helped you. If you mark a answer as a Best Answer, the user who made the post gets a bonus +12 forum points for that topic. It also helps with the community answer filtering I think
Thank you for understanding. ~Alex
1 Answer
Alexander Davison
65,469 PointsThe challenge told you to remove the for
loop but keep it's contents.
Also, you should return the FizzBuzz, Fizz and Buzz messages, not print them. :)
I hope this helps.
If you continue to fail, please respond.
~Alex
EDITED
Zhenia Ps
iOS Development Techdegree Student 407 PointsThanks Alex. It's my inattentiveness ;) I have removed for loop, here is the code:
if n % 3 == 0 && n % 5 == 0 {
print("FizzBuzz")
} else if n % 5 == 0 {
print("Buzz")
} else if n % 3 == 0 {
print("Fizz")
}
However I still receive the following error, not sure why it does not like Fizz ;))
Bummer! Double check your logic for Fizz values and make sure you're returning the correct string!
Alexander Davison
65,469 PointsUpdated my post :)
Check my post again. I also noticed something else that is causing another problem.
Zhenia Ps
iOS Development Techdegree Student 407 PointsZhenia Ps
iOS Development Techdegree Student 407 PointsWohoo ;) it worked, thanks a bunch. So return is a kind of a command. I thought that print command will return something.. lol ;)
Thanks again