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 trialJericoe States
3,262 PointsFizzBuzz
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) {
return("FizzBuzz")
} else if (n % 3 == 0) {
print("Fizz")
} else if (n % 5 == 0) {
print("Buzz")
} else {
}
return "\(n)"
}
}
// I don't know what I'm doing Wong? Can someone help?
func fizzBuzz(n: Int) -> String {
for n in 1...100 {
if (n % 3 == 0) && (n % 5 == 0) {
return("FizzBuzz")
} else if (n % 3 == 0) {
print("Fizz")
} else if (n % 5 == 0) {
print("Buzz")
} else {
}
return "\(n)"
}
3 Answers
Ryan Sady
20,594 PointsYou have a few different things going on here...
First of all, your function should return an Int, not a String.
Next, you want to print("FizzBuzz")
, not return
it.
You're also missing a closing brace at the end, but that was because you had the return declaration in the wrong part. Here's the correct code...
func fizzBuzz(n: Int) -> Int {
for n 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)")
}
}
return n
}
And to call the function:
fizzBuzz(n: 100)
Jericoe States
3,262 Pointsfunc fizzBuzz(n: 100) -> Int {
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
}
// something like that ?
Ryan Sady
20,594 PointsYou don't want to return the "Fizz" and "Buzz". You need to return n
to the main function for the input variable. You need to print
"Fizz" and "Buzz". Also when declaring a function, you don't put in your variables value. When declaring the function, it should read n: Int
.
Your code should look exactly like the code I provided.