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 trialboris said
3,607 PointsMy code is exactly the same as the recommended code, but it does still not work. It also works perfect in x-code.
So my code works perfect in Xcode and the values make sense yet the challenge won't accept it. Can someone please help me. Thank you in advanced.
func fizzBuzz(n: Int) -> String {
for n in 1...100 {
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)"
}
}
return "\(n)"
}
2 Answers
Anjali Pasupathy
28,883 PointsYou're pretty close. Your if-else statements are correctly formatted - you just need to get rid of the for loop and the final return statement.
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 hope this helps!
boris said
3,607 PointsThank you so much.
Anjali Pasupathy
28,883 PointsYou're welcome!
Jason Anders
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsHey Boris,
Anjali's answer below is correct.
Remember that the challenges are extremely specific and picky when it comes to its instructions. In this instance, the instructions do say not to use a loop and to not have a default
else statement
, so even though your code is correct in syntax, it is wrong based on the instructions. I see this a lot in the Community, so if you are getting the "Bummer!" but are sure your code is correct. Double check the instructions to make sure they were followed exactly.Keep Coding! :)