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

Correct code is not working

I have entered the code correctly but I am getting the error "Bummer! Double check your logic for Fizz values and make sure you're returning the correct string!" Any idea what this is on about?

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

2 Answers

Hi Phil,

Firstly, the question states The challenge also does not need you to loop over a range of values (using for or while). I'll take care of that.

The question is particular about Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz".

Also, Change your variable/constant name that you are checking in each step to n

Lastly, Do not worry about the default case (where the number doesn't match Fizz, Buzz, or FizzBuzz)

Just put your if statements within the code comments; no for loop, no prints; just the if logic. Your code works, but the above needs fixing.

Try changing that and see what issues you see then.

Let me know how you get on.

Steve.

Hi Steve,

I made the changes and it worked. I think Iā€™m trying to over think some to these answers. Really appreciate your help!