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

FizzBuzz

Hey guys. Not sure why it is not working here with func. I have tested my code in Playground and it was compiled just fine.

Any help is much appreciated.

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

Wohoo ;) it worked, thanks a bunch. So return is a kind of a command. I thought that print command will return something.. lol ;)

Thanks again

The print function just prints some value onto the screen and the return keyword is used for returning values.

Even built-in functions like print actually have a return value. The print function simply returns nil (basically a Swift way of saying "nothing").


Meanwhile, it would be nice to click the Best Answer button located under an answer that has helped you. If you mark a answer as a Best Answer, the user who made the post gets a bonus +12 forum points for that topic. It also helps with the community answer filtering I think :smile:

Thank you for understanding. ~Alex

1 Answer

The challenge told you to remove the for loop but keep it's contents.

Also, you should return the FizzBuzz, Fizz and Buzz messages, not print them. :)

I hope this helps.

If you continue to fail, please respond.

~Alex

EDITED

Thanks Alex. It's my inattentiveness ;) I have removed for loop, here is the code:

if n % 3 == 0 && n % 5 == 0 {
    print("FizzBuzz")
} else if n % 5 == 0 {
    print("Buzz")
} else if n % 3 == 0 {
    print("Fizz")

}

However I still receive the following error, not sure why it does not like Fizz ;))

Bummer! Double check your logic for Fizz values and make sure you're returning the correct string!

Updated my post :)

Check my post again. I also noticed something else that is causing another problem.