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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

Is editor evaluating correctly. Please, advise

I think I have the correct solution, but the editor states that I have to double check my Fizz logic. Anyone?

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  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)
        }
    }
  return "\(n)"
}

2 Answers

Hi Michael,

Essentially, your logic is fine; your if-else statements will get the job done on their own. But, our domain, in this case, is the one that the Challenge provides. And according to the challenge, the provided function takes an integer and your only job is to check if that integer is fizz, buzz or fizzbuzz and return a string accordingly.

The reason your code is not passing is that you're not doing any of these things: instead of returning strings you're printing stuff; instead of dealing only with the n argument that is passed into the function, you're creating your own range (1..100) and your own variable (i).

Fix those things and you'll pass that Challenge :)

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

Hi Mikis, thank you for your response it is much appreciated. I will go ahead and start working on the feedback you provided me.

If I may ask - this particular challenge came with a video providing one possible answer. At first the solution to this challenge did not make sense to me because it did not match the assignment entirely, but moreover, when entering the code into the editor it did not approve the solution.

So, as you understand, I am a little confused because at first I had a switch statement turning on "n".

Could you please provide me with your thoughts on this situation. Thanks!