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

code works perfect in xcode, but wont pass when i copy and paste on to web page?? says i need to check logic values...

tells me to check logic for fizzbuzz values and make sure it is returning correct string??

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {

   if n % 3 == 0 {
        return "Fizz"
    }
    else if n % 5 == 0 {
        return "Buzz"
    }
    else if n % 3 != 0 && n % 5 != 0 {
        return "FizzBuzz"
    }


  return "\(n)"
}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Ivan,

Actually the code won't work "perfectly" in Xcode. If you pass in a value of 60, you will only "Fizz" when you should get "FizzBuzz".

The problem lies with your logic setup. 60 will return true after the first if statement, and then the execution will stop. Even though 60 is also divisible by 5, it will never reach that condition.

With this in mind, the condition check for "FizzBuzz" needs to be the first condition you check. Make sense?

Keep Coding! :)

:dizzy:

THANKS! very helpful. worked after corrections you mentioned.