Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jomar Martinez
PLUS
Jomar Martinez
Pro 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,158 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

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!