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

Yashim Greene
Yashim Greene
866 Points

FizzBuzz

Ok,

What am I doing wrong here. Keep in mind I have been trying to solve this for 3 days. I am using the exact same syntax that is in the answer video. Which I almost came up with on my own forgot the "for n in 1...100" part. But my issue is I am getting the error: "Bummer! Double check your logic for Fizz values and make sure you're retuning the correct string!"

Using the following code:

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

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Yashim,

You're probably going to kick yourself when I tell you what's wrong...

With the actual code, you have nothing wrong. However, you missed one vital instruction for the challenge: "Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz". You still have print statements.

And while it will pass with the final else statement, the challenge also specifically says to not include it, as a final return is provided.

Have a look :)

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)"
}

Keep Coding! :dizzy:

Yashim Greene
Yashim Greene
866 Points

REALLY!!!!!!!

Thanks for the heads up.

Anthia Tillbury
Anthia Tillbury
3,388 Points

I tried approaching the problem over a couple of days with a Case statement:

func fizzBuzz(n: Int) -> String {

    for (n) in 1...100 {
        switch n {
        case n % 3 == 0: return "Fizz"
        case n % 5 == 0: return "Buzz"
        case n % 3 == 0 && n % 5 == 0: return "FizzBuzz"
            // End code
        }
    }

    return "\(n)"
}

I got a Bool issue, but aside from that, what is the advantage of using an IF statement over Case?

nb. I know I don't have a "default"