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

My solution to the FizzBuzz question - how to enter it into the editor properly?

So, I know my code works to solve the problem (Verified it myself in the playground), but there's a couple things I want to ask.

First, is there an easier way to select a random number other than doing the whole arc4random thing? Since I have no clue what that is or how to write it myself, I just copy-pasted it from an earlier lesson and gave it a cap of 100.

Second, I know this code works, but how do I input it according to Pasan's instructions?

//HIS CODE: 
func fizzBuzz(n: Int) -> String {
//My code
import Foundation

var n = Int (arc4random_uniform(UInt32(100)))

if n%6 == 0 {
    return "Fizz"
} else if n%5 == 0 {
    return "Buzz"
} else if (n%3 == 0) && (n%5 == 0) {
    return "FizzBuzz"
} else {
    print(n)
}
//HIS CLOSING TAGS
  return "\(n)"
}

His instructions:

Step 1: Copy-paste your code in between the comments shown below. Your solution is going inside a function I created. Don't worry about what it does, this just allows me to 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). You also don't need to define n. It is defined in the function provided.

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

(Especially confused about this, as I can't just remove my "Else" part of the if/else statement) Note: Do not worry about the default case (where the number doesn't match Fizz, Buzz, or FizzBuzz). The code in the challenge editor already takes care of that by returning the number as a string using string interpolation.

1 Answer

Cohen, here's what I did and it passed the challenge edits:

func fizzBuzz(n: Int) -> String {

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

Paean said he'd call the function, so you don't need this part:

for n in 1...100 {
    print(fizzBuzz(n))
}

You also don't need the random number, since he plans to call your function with the numbers from 1 to 100 in his loop. You don't need to import anything. The editor takes care of that, and if you did, import statements always have to be the very first statements in your code. Not sure where your n % 6 came from, as the needed divisors are just 3 and 5. It looks like a typo for n % 3. Also, you need to do this test first:

(n%3 == 0) && (n%5 == 0)

Because if either n % 3 or n % 5 is true your code will never get to this line as either your if... or your first else if... will be true. You probably will also need to leave out the print(n) part, because he asked you to let him do the last, else..., part, and the workspace is really picky about getting exactly what is asked for.