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

Jomar Martinez
PLUS
Jomar Martinez
Courses Plus Student 2,003 Points

fizzBuzz

I don't know why it's telling me to check the logic of my code. I used the same code of the answer of Pasan but it's still giving me errors. Any hints??

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  for i in 1...n {
        if (i % 3 == 0) && (i % 5 == 0)  {
            print("FizzBuzz")
        } else if (i % 3 == 0) {
           print("Fizz")
        } else if (i % 5 == 0) {
            print("Buzz")
        } else {
            print(i)
        }
    }

  // End code
  return "\(n)"
}
Nathan Tallack
Nathan Tallack
22,160 Points

Your logic is good, but you missed a few steps. Go back and read the steps in the challenge again. One calls for you to use a different variable name, and another calls for you to change your print statements to return statements.

Give it another go and do come back to us if you still need more help. :)

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Jomar:

The problem here is that you are using Pasan's example of how HE did it. Using his code, will not pass the challenge, because on the challenge there are specific steps you need to take in order for the function to work.

For example, changing your variable to n, and changing print statements to return. The reason why he has it in a function is to be able to check on different solutions, for example, instead of using IF statements, you may use a switch statement.

So for the sake of guiding you to the solution, and not giving you the solution itself I recommend going back and reading the steps once again. And to help you even further, get rid of the for in loop, and the "else" clause statement.

Just use the if conditional statements and you should be good. If you need further instructions, please let us know.

Good Luck

Jomar Martinez
Jomar Martinez
Courses Plus Student 2,003 Points

Great! it worked very well! Thanks for the help! At first I used my own code but then changed it to his because I couldn't figure it out. I should've read the instructions one more time. This time i read the instructions again and read yours. Thank you again!