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

What did I do wrong?

So.. I watched the solution video and then literally typed character by character into my Xcode (which ran) and then copied from the second line to the second to last line and I am am getting an error message. What did I do wrong is the the challenge questions compiler a little off?

Thanks!

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

  // End code
  return "\(n)"
}

2 Answers

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points
func fizzBuzz(n: Int) -> String {
    // Enter your code between the two comment markers
    var s: String = " "

    if( n % 3 == 0) && (n % 5 == 0) {
        s =  "FizzBuzz"
    } else if ( n % 3 == 0) {
        s = "Fizz"
    } else if (n % 5 == 0) {
        s = ("Buzz")
    }
    // End code
    return "\(s)"
}

The challenge asked you to return a string. In your code you are returning the number you provided as a parameter, (n : Int), formatted as a string. Your function should return a string, however that string should be based upon whether the number you provide (n : Int) is divisible by 3 and 5 returning "FIzzBuzz", divisible by just 3 returning "Fizz" or divisible by just 5 returning "Buzz".

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

Edit the return statement

return "\(n)"

and change it to

return "\(s)"