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

Unable to get my code to wotk o FizzBuzz challenge in swift 2.0

The code works in xcode

for count in 1...100 { if (count%3 == 0) && (count%5 == 0) { print("FizzBuzz") } else if (count % 3 == 0){ print("Fizz") } else if (count % 7 == 0) { print("Buzz") } else { print(count) } }

however I am not able to translate it correctly for the challenge...please help someone!

fizzBuzz.swift
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 % 7 == 0) {
        return "Buzz"
    } else {
        return "\(n)"
    }
  // End code
  //return "\(n)"
}

4 Answers

Karsten Sørensen
Karsten Sørensen
2,274 Points

Hi Nad

The reason your code is working in Xcode and not in the challenge is because your code is actually correct, although it isn't the answer for the challenge as the TeamTreeHouse compiler checks for.

When checking for the "Buzz", you should check for the remainder of 5 not 7, so the code should look like this instead

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"
    } else {
        return "\(n)"
    }
  // End code
  //return "\(n)"
}

This code is working - hope it helps

thomas bethell
thomas bethell
7,772 Points

I did not realize the "FizzBuzz" if statement had to run fist. Thanks for this!

How Jocular!

Thanks very much for the help guys....that's an important lesson i learned.