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

can someone tell me whats wrong

I'm suppose to create a function call FizzBuzz

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
// Enter your code between the two comment markers
  for i 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)
     }
  // End code
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Yes, you are. But the problem here is that the "devil is in the details" so to speak. The instructions say explicitly on step 3 to change all print statements to return, and further down it says to not loop over a range (they'll take care of that). This allows Treehouse to send your data specific information to determine if your code works (which is likely why you return the value instead of print it). Take a look at the solution:

func fizzBuzz(n: Int) -> String {

    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)"
    }
}

I cannot stress enough how important it is when doing a code challenge to read the instructions. I've also fallen victim to this and had to stop and say to myself: "Okay, I know what I thought they wanted me to do. But what did they ACTUALLY say to do?". Rarely are what I think they said and what they actually said the same thing :)

thanks so much!