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 trialCobie Fisher
4,555 PointsObjective doesn't work
I copied out the "solution" into the objective and it turns out that even the solution provided by Pasan doesn't seem to work or be accepted?!
11 Answers
Josue Gisber
Courses Plus Student 9,332 Pointsif n % 3 == 0 && n % 5 == 0 {
print("FizzBuzz")
} else if n % 5 == 0 {
print("Buzz")
} else if n % 3 == 0 {
print("Fizz")
} else {
// End code
print ("\(n)")
}
Josue Gisber
Courses Plus Student 9,332 PointsThis is what you need..
Oliver Pritchard-Barrett
3,323 Pointsswitch true {
case n % 3 == 0 && n % 5 == 0 : print("FizzBuzz")
case n % 3 == 0 : print("Fizz")
case n % 5 == 0 : print("Buzz")
default: print(n)
}
This seems to work the same??
shaunboleh
14,970 PointsThanks Pasan, I guess I am fighting with my own reading skills. Completed.
Pasan Premaratne
Treehouse TeacherDid you follow the instructions in the challenge description? You can't copy the solution directly from the video into the challenge editor.
shaunboleh
14,970 PointsThis doesn't pass the Treehouse test but works fine in my Playground. I am fighting with internet in China and have to deal with not only frustrating objectives but terrible connection. What's wrong with the code please?
for n in 1...100{
if(n % 5 == 0 && n % 3 == 0){
return "FizzBuzz"
}
else if (n % 5 == 0){
return "Buzz"
}
else if (n % 3 == 0){
return "Fizz"
}
}
Pasan Premaratne
Treehouse TeacherThe challenge did not ask you to nest the code in a for loop. I mention in the video that I'm also just using the for loop to show you how the code behaves for many different values.
To pass the test just include the if else conditional code not the for loop
Marcel Barthold
1,640 PointsWhat do I have to write to return the number?
for n in 1...100{
if (n % 3 == 0) {
return "Fizz"
} else if (n % 5 == 0) {
return "Buzz"
} else if (n % 5 == 0) && (n % 3 == 0){
return "FizzBuzz"
} else {
return "\(n)"
}
}
doesnt work
shaunboleh
14,970 PointsMarcel the for loop is not needed at all. Just the If statement. Also don't need the default case (else).
Marcel Barthold
1,640 PointsOh ... sure! Thanks!
Cobie Fisher
4,555 PointsThank you all for your feedback! The best answer was from Josue Gisber, just replace the word print with the word return and it should pass!
Adam Mazurick
Courses Plus Student 7,653 PointsSolution
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 % 5 == 0 {
return "Buzz"
} else if n % 3 == 0 {
return "Fizz"
} else {
}
// End code
return "\(n)"
}
Christoph Hellmuth
11,604 Pointsfunc fizzBuzz(n: Int) -> String { // Enter your code between the two comment markers var x:Int = 0
if (n % 3 == 0) && (n % 5 == 0) {
x = 0
} else if (n % 5 == 0) {
x = 1
} else if (n % 3 == 0) {
x = 2
}
switch x {
case 0: return "FizzBuzz"
case 1: return "Buzz"
case 2: return "Fizz"
default: return "NoBuzz"
}
// End code
return "\(n)"
}
fizzBuzz(18)
Josue Gisber
Courses Plus Student 9,332 PointsCheck the answer that I posted a month ago
Azin Mehrnoosh
13,754 PointsI was having a problem with the editor thinking that everything after the final " in the provided code was part of a string. Because of this, the solutions that worked in Xcode didn't work for this problem. I got around this by creating the following workaround. It's a lot of extra (and unnecessary) code, but if you run into the same problem I did and you really just want to check of the lesson then this might help.
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
var answer = ""
if (n % 3 == 0 && n % 5 == 0) {
answer = "FizzBuzz"
print(answer)
} else if (n % 5 == 0) {
answer = "Buzz"
print(answer)
} else if (n % 3 == 0) {
answer = "Fizz"
print(answer)
} else {
print(n)
}
return (answer)
}
tonys
12,963 Pointstonys
12,963 PointsMake sure you follow step 3 of the instructions and replace your 'print' commands with 'return' commands.