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

NATHAN TRAN
NATHAN TRAN
2,367 Points

What am I doing wrong?

What am I doing wrong? Where is my error? My compiler is right on xCode.

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
 for i in 1...100 {
    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)"
}

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Nathan,

While your code is correct and will run in Xcode, you didn't follow any of the very specific instructions the challenge has given, and this is why you are receiving the Bummer!

  1. "The challenge also does not need you to loop over a range of values (using for or while). I'll take care of that." -- So, you need to remove the for loop.
  2. "Change your variable/constant name that you are checking in each step to n." -- You will need to change all the variables you named i back to n.
  3. "Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz"." -- self-explanatory.
  4. "Do not worry about the default case" -- You'll need to delete the final else clause.

Challenges are very specific and extremely picky, so you'll always need to follow the instructions exactly. Once you fix up your code to satisfy the instructions, you're all good to go. :)

Keep Coding! :dizzy:

NATHAN TRAN
NATHAN TRAN
2,367 Points

I did this:

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

They program still tells me that it is wrong:

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

What is wrong with my "Fizz" value?

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

You're still using print when the challenge explicitly says to use return.

NATHAN TRAN
NATHAN TRAN
2,367 Points

Got it! Thanks Jason!!