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

Haitham Alam
Haitham Alam
2,685 Points

It does't work

why it doesn't work?

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

4 Answers

You have the correct logic for figuring out the fizz buzz challenge. Great job. According to the instructions you don't need to worry about the loop. Just the logic. Strip out everything except the logic from your answer:

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)"
}
Haitham Alam
Haitham Alam
2,685 Points

Thanks, i have just put the answer between the comments as the instruction says.

Sorry. I haven't done that particular challenge yet. According to the instructions you don't need to worry about the loop. Just the logic. Strip out everything except the logic from your answer:

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

Hope that helps.

Haitham Alam
Haitham Alam
2,685 Points

by the way, your is not working as well :D

Yeah. I didn't do that challenge before answering. Try my updated answer. Let me know if that helps out. Thanks.

Glad it worked. If you don't mind, could mark the answer as correct so others can benefit from it. Thanks and good luck!