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

Ryan Maneo
Ryan Maneo
4,342 Points

I thought it was this?

I'm confused... this:

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

worked fine in playground. But when I add it to this challenge it doesn't work.

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey there,

let's walk through this step by step. The corresponding assignments of this challenge are in brackets.

First of all, as your code is now supposed to be used within a function that already provides n you can get rid of this loop (You also don't need to define n. It is defined in the function provided):

for n in 1...100 {
   // ...
}

Secondly, the function is expected to return a String instead of printing to the console, indicated by -> String. (Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz")

func fizzBuzz(n: Int) -> String {
    if (n % 3 == 0) && (n % 5 == 0) {
        return "FizzBuzz"
    } else if (n % 3 == 0){
        return "Buzz"
    } else if (n % 5 == 0){
        return "Fizz"
    } else {
        return ""
    }
}

Thirdly, "Buzz" and "Fizz" conditions are mixed up, they have to be changed to this:

func fizzBuzz(n: Int) -> String {
    if (n % 3 == 0) && (n % 5 == 0) {
        return "FizzBuzz"
    } else if (n % 5 == 0){
        return "Buzz"
    } else if (n % 3 == 0){
        return "Fizz"
    } else {
        return ""
    }
}

This way, you should pass the challenge. Hope that helps :)

Ryan Maneo
Ryan Maneo
4,342 Points

But functions weren't even taught before that challenge... functions were in the next course...

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

I only answered the question you asked in combination with the assignment you linked. This is out of context, so I did not know functions weren't covered yet. I am not responsible for the tracks here, just trying to help out ;)

Ryan Maneo
Ryan Maneo
4,342 Points

You did help and thank you. I can easily do it now that I've started learning functions... maybe he wanted us to go back and do it? Anyway thank you again!