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 trialBilly Potts
Courses Plus Student 2,344 Pointscan someone tell me whats wrong
I'm suppose to create a function call FizzBuzz
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
for i in 1...100 {
if (n % 3 == 0) && (n % 5 == 0) {
print("FizzBuzz")
} else if (n % 3 == 0) {
print("Fizz")
} else if (n % 5 == 0) {
print("Buzz")
} else {
print(n)
}
// End code
}
1 Answer
Jennifer Nordell
Treehouse TeacherYes, you are. But the problem here is that the "devil is in the details" so to speak. The instructions say explicitly on step 3 to change all print statements to return, and further down it says to not loop over a range (they'll take care of that). This allows Treehouse to send your data specific information to determine if your code works (which is likely why you return the value instead of print it). Take a look at the solution:
func fizzBuzz(n: Int) -> String {
if n % 3 == 0 && n % 5 == 0 {
return "FizzBuzz"
} else if n % 3 == 0 {
return "Fizz"
} else if n % 5 == 0 {
return "Buzz"
} else {
return "\(n)"
}
}
I cannot stress enough how important it is when doing a code challenge to read the instructions. I've also fallen victim to this and had to stop and say to myself: "Okay, I know what I thought they wanted me to do. But what did they ACTUALLY say to do?". Rarely are what I think they said and what they actually said the same thing :)
Billy Potts
Courses Plus Student 2,344 PointsBilly Potts
Courses Plus Student 2,344 Pointsthanks so much!