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 Collections and Control Flow Control Flow With Conditional Statements FizzBuzz Challenge

Martel Storm
Martel Storm
4,157 Points

Why can't I pass? Code works

After I click on Check work I get

"XXX Bummer! Double check your logic for Fizz values and make sure you're returning the correct string!"

Is something uppercase that shouldn't be? Thank you so very much in advance!

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
      for n in 1...100 {
        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")}}
  // End code
  return "\(n)"
}
Martel Storm
Martel Storm
4,157 Points

Now it's telling me "Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors." and there is no errors being displayed

1 Answer

andren
andren
28,558 Points

Your code does not fulfill some of the criteria that the challenge instructions laid out for adapting your solution to the task, namely these ones:

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.

The challenge also does not need you to loop over a range of values (using for or while). I'll take care of that.

You do cover the default case with your else statement, though that is not actually what causes your code to not fulfill the challenge, that is caused by the fact that you are looping over a range of values in your solution. Which you should not do because the challenge is setup to provide you with the numbers though a function parameter.

If you remove the loop (and 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 your code will pass.