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

Ralph Nicholson
Ralph Nicholson
812 Points

Stuck on FizzBuzz Question - Swift 2.0 Collections and Control Flow!

I'm not sure where I'm going wrong, compiler error I'm receiving in treehouse is:

swift_lint.swift:3:33: note: to match this opening '{' func fizzBuzz(n: Int) -> String {

When I run the code in Xcode I receive an error stating - "Will never be executed"

I feel like it something very simple and I am just not seeing it. Thanks for the help!

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) {
    return "FizzBuzz"
} else if (n % 3 == 0) {
    return "Fizz"
} else if (n % 5 == 0) {
    return "Buzz"
} else {
    return "No Fizzing or Buzzing here!"
}
  // End code
  return "\(n)"
}

1 Answer

First, you started a for loop inside your function that's not needed, and which doesn't have a closing curly brace. If you check the instructions, it says "The challenge also does not need you to loop over a range of values (using for or while). I'll take care of that."

Second, the else... clause was supposed to return n as a String. The challenge editor is quick picky sometimes, and I'm sure that it wasn't happy with that either.

Here's the code, so you can compare:

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