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

Wojciech Pilwinski
Wojciech Pilwinski
1,723 Points

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

Hello, I have problem with "FizzBuzz" exercise. I solved the task on my own but I receive error. And strange thing is, even I copy/paste exact the same code as in your video with your solution I receive the same error. And off course code works properly in my xCode app on my computer. So I claim there is some problem with the machine on teamtreehouse. . Can you help me? Because I can't pass the course without that exercise. And by the way, can you give me advice which course would be the best as next after "Collections and Control Flow. Please notice I'm begginer.

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("Fizz") }
        else { print(i) }
    }
  // End code
  return "\(n)"
}

1 Answer

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey there,

like the instructions say you have to modify your code a bit so it can be verified correctly. Firstly you don't have to use a for in loop but you just have to check the different cases (which you could also do with a switch block with different cases). Also make sure to change the name of the constant that is checked to n because that is what's passed to the function that verifies the code.

Then you should return the strings instead of printing them and also note that you don't need to print the number yourself as this is done in the end already. Lastly also make sure to return "Buzz" if n % 5 is 0 because right now you have "Fizz" two times.

If you fix those issues it should look like this and work.

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

I hope that helps, if you have further questions feel free to ask! :)