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

Objective doesn't work

I copied out the "solution" into the objective and it turns out that even the solution provided by Pasan doesn't seem to work or be accepted?!

tonys
tonys
12,963 Points

Make sure you follow step 3 of the instructions and replace your 'print' commands with 'return' commands.

11 Answers

Josue Gisber
PLUS
Josue Gisber
Courses Plus Student 9,332 Points
if n % 3 == 0 && n % 5 == 0 {
        print("FizzBuzz")
    } else if n % 5 == 0 {
        print("Buzz")
    } else if n % 3 == 0  {
        print("Fizz")
    } else {
        // End code
        print ("\(n)")
    }
Oliver Pritchard-Barrett
Oliver Pritchard-Barrett
3,323 Points
switch true {
    case n % 3 == 0 && n % 5 == 0 : print("FizzBuzz")
    case n % 3 == 0 : print("Fizz")
    case n % 5 == 0 : print("Buzz")
    default: print(n)
}

This seems to work the same??

Thanks Pasan, I guess I am fighting with my own reading skills. Completed.

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

Did you follow the instructions in the challenge description? You can't copy the solution directly from the video into the challenge editor.

This doesn't pass the Treehouse test but works fine in my Playground. I am fighting with internet in China and have to deal with not only frustrating objectives but terrible connection. What's wrong with the code please?

   for n in 1...100{
        if(n % 5 == 0 && n % 3 == 0){
            return "FizzBuzz"
        }
        else if (n % 5 == 0){
            return "Buzz"
        }
        else if (n % 3 == 0){
            return "Fizz"
        }
    }
Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

The challenge did not ask you to nest the code in a for loop. I mention in the video that I'm also just using the for loop to show you how the code behaves for many different values.

To pass the test just include the if else conditional code not the for loop

Marcel Barthold
Marcel Barthold
1,640 Points

What do I have to write to return the number?

for n in 1...100{
    if (n % 3 == 0) {
        return "Fizz"
    } else if (n % 5 == 0) {
        return "Buzz"
    } else if (n % 5 == 0) && (n % 3 == 0){
        return "FizzBuzz"
    } else {
           return "\(n)"
    }
}

doesnt work

Marcel the for loop is not needed at all. Just the If statement. Also don't need the default case (else).

Thank you all for your feedback! The best answer was from Josue Gisber, just replace the word print with the word return and it should pass!

Solution

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

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

    }

  // End code
  return "\(n)"
}
Christoph Hellmuth
Christoph Hellmuth
11,604 Points

func fizzBuzz(n: Int) -> String { // Enter your code between the two comment markers var x:Int = 0

if (n % 3 == 0) && (n % 5 == 0) {
    x = 0
} else if (n % 5 == 0) {
    x = 1
} else if (n % 3 == 0) {
    x = 2
}
switch x {
    case 0: return "FizzBuzz"
    case 1: return "Buzz"
    case 2: return "Fizz"
default: return "NoBuzz"
}
// End code
return "\(n)"

}

fizzBuzz(18)

I was having a problem with the editor thinking that everything after the final " in the provided code was part of a string. Because of this, the solutions that worked in Xcode didn't work for this problem. I got around this by creating the following workaround. It's a lot of extra (and unnecessary) code, but if you run into the same problem I did and you really just want to check of the lesson then this might help.

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