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 trial

iOS Swift 2.0 Collections and Control Flow Control Flow With Conditional Statements FizzBuzz

Anuradha Nath
seal-mask
.a{fill-rule:evenodd;}techdegree
Anuradha Nath
iOS Development Techdegree Student 2,686 Points

I 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.

fizzBuzz.swift
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)"
  }
}
Anuradha Nath
seal-mask
.a{fill-rule:evenodd;}techdegree
Anuradha Nath
iOS Development Techdegree Student 2,686 Points

I 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.

1 Answer

You 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

Oops I didn't see your little comment below your answer saying you already found the answer. LOL :)