Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Conditional Statements FizzBuzz Challenge

Edward Sapp
Edward Sapp
8,479 Points

Why isn't my tuple switch working? It works in Xcode!

func fizzBuzz(n: Int) -> String { // Enter your code between the two comment markers switch (n % 3, n % 5){ case (0, ): return "Fizz" case(, 0): return "Buzz" case(0, 0): return "FizzBuzz" default: break } // End code return "(n)" }

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
  switch (n % 3, n % 5){
  case (0, _):
    return "Fizz"
  case(_, 0):
    return "Buzz"
  case(0, 0):
    return "FizzBuzz"
  default:
    break
  }
  // End code
  return "\(n)"
}

1 Answer

Hello :

The way you have it, won't reach the "FizzBuzz" case, so if you put it on top, it will evaluate all possible conditions better..

For example if you pass it the number 15, since it could be a number that will trigger "fizz" or "buzz" conditions, then it won't reach all the way to the last case. So, it's best if you put the "FizzBuzz" case as the first condition.

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

  case(0, 0):
    return "FizzBuzz"
  case (0, _):
    return "Fizz"
  case(_, 0):
    return "Buzz"

  default:
    break
  }
  // End code
  return "\(n)"
}

Hope it goes well, good luck.

Edward Sapp
Edward Sapp
8,479 Points

Thanks Jhoan. My biggest question is, what does "case (0, _)" mean? The way I read it, is something akin to:

"The case in which n%3==0 and n%5!=0"

In this case, it wouldn't matter whether the FizzBuzz condition was at the top or bottom. Thus, I must be reading something incorrectly, probably the _ symbol. What does the _ symbol represent in this case?

A tuple holds 2 values or more, if you want to ignore a specific one, you use the _ . So in this case it's evaluating one, but not the other.

Since you are switching on the tuple, it considers both values, unless you ignore one or the other.

// Tuple 

case (_,0 ) // evaluates the right value
case (0,_) // evaluates the left value
case (0,0) // evaluates both 

Take a look at this example:

let tuple = ("Hello", "Bye")

tuple.1 // Prints Bye
tuple.0 // Prints Hello

// You can name the individual elements in a tuple.

let tuple2 = (color: "Yellow" , car: "Ford")

tuple2.car  // Prints Ford
tuple2.color // Prints Yellow


// Using a switch

switch tuple2 {
case (_, "Car"):
  print("This is a car")
case ("Yellow", _ ) :
  print("This is a color") // Will print this is a color
default:
  break
}

It will print this is a color, because the value that I passed in the case "Car" is not the same as " Ford ".

Good luck