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 trialRyan Maneo
4,342 PointsI thought it was this?
I'm confused... this:
for n in 1...100 {
if (n % 3 == 0) && (n % 5 == 0) {
print("FizzBuzz")
} else if (n % 3 == 0){
print("Buzz")
} else if (n % 5 == 0){
print("Fizz")
} else {
print(n)
}
}
worked fine in playground. But when I add it to this challenge it doesn't work.
1 Answer
Martin Wildfeuer
Courses Plus Student 11,071 PointsHey there,
let's walk through this step by step. The corresponding assignments of this challenge are in brackets.
First of all, as your code is now supposed to be used within a function that already provides n
you can get rid of this loop (You also don't need to define n. It is defined in the function provided):
for n in 1...100 {
// ...
}
Secondly, the function is expected to return a String instead of printing to the console, indicated by -> String
.
(Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz")
func fizzBuzz(n: Int) -> String {
if (n % 3 == 0) && (n % 5 == 0) {
return "FizzBuzz"
} else if (n % 3 == 0){
return "Buzz"
} else if (n % 5 == 0){
return "Fizz"
} else {
return ""
}
}
Thirdly, "Buzz" and "Fizz" conditions are mixed up, they have to be changed to this:
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 {
return ""
}
}
This way, you should pass the challenge. Hope that helps :)
Ryan Maneo
4,342 PointsRyan Maneo
4,342 PointsBut functions weren't even taught before that challenge... functions were in the next course...
Martin Wildfeuer
Courses Plus Student 11,071 PointsMartin Wildfeuer
Courses Plus Student 11,071 PointsI only answered the question you asked in combination with the assignment you linked. This is out of context, so I did not know functions weren't covered yet. I am not responsible for the tracks here, just trying to help out ;)
Ryan Maneo
4,342 PointsRyan Maneo
4,342 PointsYou did help and thank you. I can easily do it now that I've started learning functions... maybe he wanted us to go back and do it? Anyway thank you again!