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

Nicholas Kennedy
Nicholas Kennedy
14,015 Points

FizzBuzz Solution works in playground but not in Treehouse Compiler

Solution is working fine in playground but wont pass in compiler. Am I missing something?

func fizzBuzz(n: Int) -> String {

var finalString = String()

for x in 1...n {

    //Numbers divisible by both become fizz buzz.
    if ( x % 3 == 0 && x % 5 == 0 ) {
        finalString += "FizzBuzz "
    } else if x % 5 == 0 {
        finalString += "Buzz "
    } else if x % 3 == 0 {
        finalString += "Fizz "
    } else {
        finalString += "\(x) "
    }

}

return finalString

}

fizzBuzz(10)

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

It's not that straightforward to figure out, but what he wants is for the function to just take in one number and return one result. It seems like this function is going to be called multiple times for each number in a range - you've implemented that within your function, but I think that part is already implemented for you on the outside in the part that we're not seeing. Also, I think they want the word with no whitespace in it.

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 % 5 == 0 {
        return "Buzz"
    } else if n % 3 == 0 {
        return "Fizz"
    }
        // End code
    return "\(n)"
}