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

Rafael Nogueira
Rafael Nogueira
664 Points

why it isn't working?

It just keep saying to double check my logic for FizzBuzz values, but in the playground everything is perfectly right.

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers

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

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey there Rafael,

While your code is correct in syntax, there is an error in the logic. Remember that if the first if statement is satisfied, the rest of the conditional won't even execute, so a number may be divisible by both 3 and 5, but your program will never find out, because if it is divisible by 3 or 5, the code will break there.

You just need to rearrange the logic in your if/else statement to check for Fizzbuzz first, then the others.

Give that a shot. Let me know. :)

Rafael Nogueira
Rafael Nogueira
664 Points

Hi Jason,

I made the adjustments and it's like this now:

func fizzBuzz(n: Int) -> String {

for var n = 1; n <= 100; n++ { if (n % 3 == 0) && (n % 5 == 0) { return "FizzBuzz" } else if (n % 3 == 0) { return "Fizz" } else if (n % 5 == 0) { return "Buzz" } }

return "(n)" }

Unfortunately i didn't work so well. The compiler keep accusing an error, so after strugling with this for a few hours i gave up and watched the resolution video and guess what, my code is right, but the compiler keeps saying it's not. Thanks for your help anyway, a didn't recall about these details.

jonlunsford
jonlunsford
15,480 Points

try getting rid of the function and implement just as an if statement.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

The function is pre-loaded code initiated by the challenge and cannot be deleted. The challenges are very picky and very strict. Any pre-loaded code that is deleted or modified (without specific instruction to do so) will give a Bummer! even if your code is correct. Also, any code added to a challenge that wasn't asked for will often result in a Bummer! too. So, you have to watch out for this... It's also very picky for spelling, capitalization, and punctuation.

:smiley: