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

Why isn't the correct answer shown in the following video correct?

I tried a switch statement and it didn't work, so I went ahead and did what the answer in the next video says, and I changed the "i" variable used in there to n, why does it say I am incorrect when the preview says that it is totally fine?

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

1 Answer

andren
andren
28,558 Points

In the challenge text this is stated:

"Enter your FizzBuzz solution here! You will have to make some minor changes to get this to work with our editor though."

As part of that there are two requirements that the code you pasted does not follow:

"Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz"."

And:

"Note: Do not worry about the default case (where the number doesn't match Fizz, Buzz, or FizzBuzz). The code in the challenge editor already takes care of that by returning the number as a string using string interpolation."

Since the code is filled with print statements instead of return statements, and also does deal with the default case, with it's else clause. It doesn't fulfill the challenge criteria.

If you change print to return and drop the else statement 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 % 3 == 0) {
        return("Fizz")
    } else if (n % 5 == 0) {
        return("Buzz")
    }
  // End code
  return "\(n)"
}

Then the code will pass the challenge.