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 Solution to FizzBuzz

Hey, everyone, I am not sure what I am doing wrong here. I can't get anything to show up in the console for the Fizzbuzz

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

}

Henry Sabio
Henry Sabio
3,680 Points

If you're trying to get something to print to the console, if I'm not mistaken you have to use "console.log()"

you are passing n into your function but never using it.

also if you are trying to run this in playground please understand it's extremely buggy and often requires a restart to get interpretation to work

Bruce Röttgers
Bruce Röttgers
18,211 Points

Henry Sabio

If you're trying to get something to print to the console, if I'm not mistaken you have to use "console.log()" No this is JS syntax. In Swift it's print()

1 Answer

Bruce Röttgers
Bruce Röttgers
18,211 Points

Hey,

reading the instructions should clear things out.

Step 1: Enter your code in between the comments shown below. The code is going inside a "function" that will help verify your solution.

Step 2: Change your variable/constant name that you are checking in each step to n. For example if (n % 3 == 0). Note: You don't need to create n, it is already provided.

Step 3: Change all your print statements to return statements. For example: print("FizzBuzz") becomes return "FizzBuzz".

Therefore the logic isn't correct. You thought the function should be called and print all numbers out. But in reality the challenge will call your function 100 times and wants to get everytime the number.

Changing your code using the steps provided above:

Original:

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

Changing constant names as declared in Step 2 (and removing for loop as I mentioned):

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

Changing print() to return as declared in Step 3:

if (n % 3 == 0) && (n % 5 == 0) {
        return "FizzBuzz"
    } else if (n % 3 == 0) {
        return "Fizz"
    } else if (n % 5 == 0) {
        return "Buzz"
    } else {
        return n
    }

This really helps! Thank you so much !