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 trialNad Naqvi
4,834 PointsUnable to get my code to wotk o FizzBuzz challenge in swift 2.0
The code works in xcode
for count in 1...100 { if (count%3 == 0) && (count%5 == 0) { print("FizzBuzz") } else if (count % 3 == 0){ print("Fizz") } else if (count % 7 == 0) { print("Buzz") } else { print(count) } }
however I am not able to translate it correctly for the challenge...please help someone!
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
if (n % 3 == 0) && (n % 5 == 0) {
return "FizzBuzz"
} else if (n % 3 == 0) {
return "Fizz"
} else if (n % 7 == 0) {
return "Buzz"
} else {
return "\(n)"
}
// End code
//return "\(n)"
}
4 Answers
Karsten Sørensen
2,274 PointsHi Nad
The reason your code is working in Xcode and not in the challenge is because your code is actually correct, although it isn't the answer for the challenge as the TeamTreeHouse compiler checks for.
When checking for the "Buzz", you should check for the remainder of 5 not 7, so the code should look like this instead
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
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)"
}
// End code
//return "\(n)"
}
This code is working - hope it helps
thomas bethell
7,772 PointsI did not realize the "FizzBuzz" if statement had to run fist. Thanks for this!
Krondor The Destroyer
1,556 PointsHow Jocular!
Nad Naqvi
4,834 PointsThanks very much for the help guys....that's an important lesson i learned.