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

Nathalie Dory
Nathalie Dory
2,912 Points

still not working

I'm pretty sure now I have copied the instructor completely and it still doesn't work

func fizzBuzz(n: Int) -> String { // Enter your code between the two comment markers for n in 1...100{

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

}

WHY!

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
      for n in 1...100{

    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

Ryan Sady
Ryan Sady
20,594 Points

Looks like youre forgetting to include the for loop in your code? I copied and pasted your code in a playground, and nothing happened until I put the

for n in 1...100 { 

Your code should look like this:

func fizzBuzz(n: Int) -> String {

    for n in 1...100{

        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)
        }
    }
    return "\(n)"
}

//Call function with int.  I used 100 for an example...

fizzBuzz(n:100)
Nathalie Dory
Nathalie Dory
2,912 Points

ah thanks for trying, but it is there just closer to the comment

Ryan Sady
Ryan Sady
20,594 Points

I'm not quite sure I understand the issue then? If you copy/paste the code in a playground, it runs with no issue... Does the code not run in your playground, or does it not accept it in the code challenge?

Nathalie Dory
Nathalie Dory
2,912 Points

Yes it was a problem with the code challenge, in the playground it didn't seem to come up with an issue. In the end I had to remove the last print(n) line and it worked..