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 Collections and Control Flow Control Flow With Conditional Statements FizzBuzz Challenge

Could someone help me with this???

My playground gets the right answer all day, but the system keeps spitting it back.. I have no idea what's up. Cause I'm getting the answer it wants....

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

fizzBuzz(n: 18)

1 Answer

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Isaac Gonzalez,

You've done a great job there. Just need to read the challenge a little more closely.
(code is fine, just does not meet challenge requirements)

func fizzBuzz(n: Int) -> String {
    // Enter your code between the two comment markers
    if n % 5 == 0 && n % 3 == 0 {
        return "Fizz Buzz" //return "FizzBuzz" not "Fizz Buzz"
    } else if n % 5 == 0 {
        return "Buzz"
    } else if n % 3 == 0 {
        return "Fizz"
    } else { 
        // you can leave this final else in if you like, but the challenge specifically says it isn't required
        // Note: Do not worry about the default case (where the number doesn't match Fizz, Buzz, or FizzBuzz).
        // The code in the challenge editor already takes care of that by returning the number as a string using string interpolation.
       return String(n)
    }
    // End code
}

fizzBuzz(n: 18)  // also not required

You're doing great! Keep going :dizzy:
Dave

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Isaac Gonzalez — You can mark the question solved by choosing a "Best Answer".
And happy coding! :sparkles: