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

Lana Wong
Lana Wong
3,968 Points

I would like to know...

I know there are many ways to do FizzBuzz, but I like to learn. :)

It always says: Bummer! Double check your logic for Buzz values and make sure you're returning the correct string!

What am I doing wrong?

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  var n = 6
if n % 3 == 0{
return "Fizz"
} else
if n % 5 == 0{
return "Buzz"
} else
if n % 3 == 0 && n % 5 == 0{
return "FizzBuzz"
} else {
return String(n)
}
  // End code
  return "\(n)"
}

1 Answer

Taylor Amsler
Taylor Amsler
4,427 Points

Hey there Lana. I can see a few things that may not be working with your code. I'm gonna start out by saying that if you aren't working in an Xcode playground, you should definitely start there as Xcode often helps me identify problems I'm having.

Anyways, on to your particular code. I'm gonna identify 3 big issues and let you take it from there. You've already got some good ideas.

1) You have a function, but you forgot to iterate through the values! Remember that, for this challenge, you want to be iterating through everything from 1 through n. The best way to do that is probably with a for-in loop. So maybe something like:

for aNumber in 1...n {
// Do things here
}

2) You have a variable that stores n (var n = 6) for when you return the function. That's great! But you are actually going to be storing many values of n and the words are not ints, but strings. So instead of a single Int property, maybe you could look at an array of strings.

3) Keep in mind that swift goes through for-in functions line by line. What I mean by this comment is that it executes the first for-in statement before the second and so on. So in your code, if the for loop was iterating through something, it would execute n%3 == 0 and n%5 == 0 BEFORE n%3 == 0 && n%5 == 0. I think that that may pose a problem, but I'm going to let you think that over for yourself.

Hope this was helpful and best of luck!