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 trialAnuradha Nath
iOS Development Techdegree Student 2,686 PointsI was writing the fizzbuzz code in the editor and I am getting following error in the treehouse editor.Please help:
code entered by me are between the comment markers: func fizzBuzz(n: Int) -> String { // Enter your code between the two comment markers if ( n%3 == 0){ return "Fizz" } else if (n%5 == 0){ return "Buzz" } else if (n%3==0 && n%5==0){ return "FizzBuzz" } else { // End code return "(n)" } }
getting following error: Double check your logic for FizzBuzz values and make sure you're returning the correct string!
Not compilation error but can't submit my answer. I tried both switch and If cases.
Thanks, Anuradha.
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
if ( n%3 == 0){
return "Fizz"
} else if (n%5 == 0){
return "Buzz"
} else if (n%3==0 && n%5==0){
return "FizzBuzz"
} else {
// End code
return "\(n)"
}
}
1 Answer
Alexander Davison
65,469 PointsYou did a good job! The bug here is very sneaky and very cleverly hid. Let's say our number that we're going to check is 15. 15 Is divisible by both 3 and 5, so it should return "FizzBuzz". But if you tried your code, it will return "Fizz". Here's why: When you run your function/method, it checks if 15 is divisible by 3 first. Since it is divisible by 3, it passes and it returns just "Fizz". So you should check if the number is divisible by 3 and 5 first. If you fix your code it should look like this:
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)"
}
}
Good luck!
Hope this helps, Alex
Alexander Davison
65,469 PointsOops I didn't see your little comment below your answer saying you already found the answer. LOL :)
Anuradha Nath
iOS Development Techdegree Student 2,686 PointsAnuradha Nath
iOS Development Techdegree Student 2,686 PointsI got it, I had to arrange my conditions to match the requirement. the above written code is logically incorrect, it will never satisfy elseif condition.
Thanks, Anuradha.