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

Melinda Caines
PLUS
Melinda Caines
Courses Plus Student 892 Points

Swift Fizzbuzz

I'm typing the solution in exactly as it is on the video and it's not working.

if (n % 3 == 0) && (n % 5 == 0){ print("FizzBuzz")

}
else if (n % 3 == 0){
    print("Fizz")

}

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

    if (n % 3 == 0) && (n % 5 == 0){
        print("FizzBuzz")

    }
    else if (n % 3 == 0){
        print("Fizz")

    }

    else if (n % 5 == 0){
        print("Buzz")
    }
    else {
        print(n)
    }


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

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing well, and the logic is fine, but challenges are extremely picky and this one more than most.

You might be correct about doing it exactly as in the video. But you're missing the specific instructions of the challenge. Ignoring these instructions will cause the challenge to fail. Here's a quote:

"Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz"."

Right now, all your print statements are intact and none have been modified to return.

Here's another quote:

"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.

Currently, you have a default case set up. Try it again paying particular attention to the instructions. You've got this! :smiley:

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

Hey Melinda,

Your code is good, but it isn't complying with the instructions correctly. The instructions mention in Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz". and they also specify 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. That said, your code should look something like this:

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 % 3 == 0){
       return "Fizz"

    }

    else if (n % 5 == 0){
        return "Buzz"
    }


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

Hope this helps, Rodrigo