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 Collections and Control Flow Control Flow With Conditional Statements FizzBuzz Challenge

meitav asulin
meitav asulin
1,123 Points

help with mission

why it is wrong

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

3 Answers

Hi there,

Do the double-test first. Otherwise, your FizzBuzz candidates will all get filtered off at the first line as n % 3 == 0.

Make sense?

Steve.

Also, if you're using a switch statement, you aren't switching on n. You are looking for a true outcome, i.e. you want n % 3 == 0 to evaluate to true else that value of n is disregarded. So switch on true then put your cases in as you have done - but think about the order, as in my other answer. Do the double test first. Something like:

   switch true {
     case n % 3 == 0 && n % 5 == 0: 
       return "FizzBuzz"

The challenge is expecting you to use multiple if and else if statements, as below. That may be the easier way forward. But both are valid solutions.

  if n % 3 == 0 && n % 5 == 0 {
    return "FizzBuzz"

I hope that helps,

Steve.

Alternatively, to switch on n, use a where clause like:

 switch n {
  case _ where n % 5 == 0 && n % 3 == 0:
    return "FizzBuzz"
meitav asulin
meitav asulin
1,123 Points

the value we consider is a name like func name or it is a value that must have some activity (function, true, false...) ?

thanks for help

This way won't work:

switch n {
  case n % 3 == 0: // do something
}

because the case evaluates to a true value boolean whereas n is an integer. So, the case will never be met as an integer isn't a boolean.

So you switch on the outcome you want to compare, which is n % 3 == 0 evaluating to true. So, here:

switch true {
  case n % 3 == 0: // do something
}

will pull out all examples where n % 3 == 0 is true and disregard the rest which is precisely what we want!

I think if you're using a switch to check for a true value, maybe an if statement is deemed more appropriate. The outcome is binary, true or false, so given that there are only three tests being made, perhaps three if statements are simpler? That's down to a matter of preference. While I think if would be the more purist solution, I'd go down the switch route too just because adding further conditions is easier and lots of if statements lack clarity.

Make sense?

Steve.

If you like complicated solutions, you could use tuples. This makes the order irrelevant as the cases are unique so doing the single tests first wouldn't filter out the double-test candidates.

However, this is horrible code - it doesn't read well at all! Complicated switch conditions aren't helpful. There are clearer ways of doing this, as above but I thought I'd mention it just to demonstrate how badly wrong things can go if you don't keep it simple!

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