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

Ravirayappan Chinnappan
PLUS
Ravirayappan Chinnappan
Courses Plus Student 11,293 Points

What is the fault in this code

correct it...

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  if(n % 3 == 0) 
  {
  print("Fizz")
  } 

  if (n % 5 == 0)
  {
  print("Buzz")
  }

  if (n % 3 == 0) && (n % 5 == 0)
  {
  print("FizzBuzz")
  }

  // End code
  return "\(n)"
}
Matt Lewis
Matt Lewis
5,970 Points

At first glance, shouldn't the second and third conditionals be "else if"?

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello :

Your code looks good, the only thing is that you have to reverse it a bit. Make sure that the FizzBuzz is the first one to be evaluated. And then also consider using "else if". Here I made 2 solutions to this challenge.

var numbers = 1...100

// Switch Statement
for number in numbers {
    switch number {
    case number where number % 3 == 0 && number % 5 == 0 :
        print("FizzBuzz")
    case number where number % 3 == 0:
        print("Fizz")
    case number where number % 5 == 0:
        print("Buzz")
    default:
        print(number)
    }
}

// If statement
for n in numbers {
    if n % 3 == 0 && num % 5 == 0{
        print("FizzBuzz")
    } else if n % 3 == 0 {
        print("Fizz")
    } else if n % 5 == 0 {
        print("Buzz")
    } else {
        print(n)
    }
}

Although I know, Pasan did not go over the where clause in the switch statement, I will post it on here, so that you can see the power of the switch statement as well.

Good luck.

Ravirayappan Chinnappan
Ravirayappan Chinnappan
Courses Plus Student 11,293 Points

hello, while running this code its giving some error, that is - "Double check your logic for Fizz values and make sure you're printing the correct string!"

Jhoan Arango
Jhoan Arango
14,575 Points

Yes, I know.. that challenge is a bit confusing. I am almost 100% certain that there is some sort of bug in the challenge. When I try to use string interpolation to "return", it would not allow me.

But please READ this guys Solution, and work with it to pass the challenge. You will notice that Pasan's explanation of his solution to the challenge is the same as the one I provided you with.

Good luck

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

Ravirayappan Chinnappan and Jhoan Arango,

The issue here is that your code includes a case for when the the number does not match either FizzBuzz, Fizz or Buzz (which is the correct way to do it). The code in the challenge already takes care of that for you. If you notice at the bottom of the code provided there is a line

return "\(n)" 

I'm using string interpolation to return the number as a string.

I've updated the directions to make it more clear that you don't have to worry about the default case.

And yes: Cheating with the where clause :smiley:

For what it's worth Jhoan, you can optimize that code by ignoring the number variable in your cases like this:

case _ where number % 3 == 0 && number % 5 == 0 :

Since you don't actually need the number in the case statement and you're performing the logic in the where clause, you can just discard the number. This allows the compiler to optimize memory by not maintaining a copy of that variable.

Jhoan Arango
Jhoan Arango
14,575 Points

Thank you Pasan Premaratne,

Thanks for the explanation with the where clause, I did not know you can optimize it like that.