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

Matthew Lurie
Matthew Lurie
3,512 Points

attempting to solve this using switch. What am I doing wrong?

just watched the video and...switch was not addressed as a possible solution. Also all the "return" statements not addressed. Also no explanation of the function that was pre-loaded...am I the only one who felt the video solution did not address the parameters of the challenge?

Any help into my coding errors would be appreciated.

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {
  // Enter your code between the two comment markers
   switch n {
    case (n % 3 == 0) && (n % 5 == 0): return "FizzBuzz"      
    case (n % 3 == 0): return "Fizz"
    case (n % 5 == 0): return "Buzz"


  // End code
  return "\(n)"
}

2 Answers

Stuart Wright
Stuart Wright
41,072 Points

I think the intended solution for this challenge is to use if/elseif/else. That would certainly be much easier than using a switch statement. That said, getting this to work with a switch statement was a fun challenge - here are the changes I made to get it to work:

  • Added a curly brace at the end of the switch statement. This was missing in your code above.
  • This is the most important one: the value that you are switching on (n) is of type int, but your case statements evaluate to type boolean. This causes a compile error. The type of the value you are switching on must match the type of the cases you define. It seems we need boolean cases here, so you should switch on a boolean. Perhaps surprisingly, simply entering true works.
  • Switch statements must be exhaustive, so you need to add the default case to the switch statement.

Here's my solution:

func fizzBuzz(n: Int) -> String {
    switch true {
        case (n % 3 == 0) && (n % 5 == 0): return "FizzBuzz"      
        case (n % 3 == 0): return "Fizz"
        case (n % 5 == 0): return "Buzz"
        default: return "\(n)"
    }
}

An alternative solution I came across by searching the forums is:

func fizzBuzz(n: Int) -> String {
  switch(n % 3 == 0, n % 5 == 0) {
      case(true, false):
          return "Fizz"
      case(false, true):
          return "Buzz"
      case(true, true):
          return "FizzBuzz"
      default: 
          return "\(n)"
  }
}

Credit: https://teamtreehouse.com/community/fizz-buzz-3

Matthew Lurie
Matthew Lurie
3,512 Points

thanks. This challenge was especially confusing because it came right after Pasan had just said that we almost always want to use switch instead of if statements. In addition, the video solution didn't actually reflect a lot of what we had just learned. For example, the "for in" statement in the video is NOT in the solution. And replacing print with return is not in the video. I see from the forums that I'm not the only one experiencing this. Just seems like there's a sloppy level of quality between what the challenges ask and what is being taught. Doesn't help anyone when one explicitly doesn't follow the other.