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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

In need of some pointers

Hi,

Since we have not covered func and return, we only needed to include the code. When including the code, Xcode informed me that there was a Bool Vs Int conflict. Could someone hint me what I can improve on.

Thanks!

fizzBuzz.swift
func fizzBuzz(n: Int) -> String {

  switch n {
     case n % 3 == 0: print("Fizz")
     case n % 5 == 0: print("Buzz")
     case n % 15 == 0: print ("FizzBuzz")
    }

  return "\(n)"
}

4 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hmmm not sure why as the code is correct, I attempted with the below and it works

  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)"
    }
Jeff McDivitt
Jeff McDivitt
23,970 Points

Here is the answer, see if you can figure out what it is doing

for i in 1...100{
    if (i & 3 == 0) && (i % 5 == 0){
        print("FizzBuzz")
    }else if i & 3 == 0{
        print("Fizz")
    }else if i % 5 == 0{
        print("Buzz")
    }else{
        print(i)
    }
}
Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

Thanks Jeff, your help is much appreciated. I also viewed the Treehouse video that explains the exact same answer and I do understand what it is doing.

However, I guess, I have trouble understanding the question in the Code Challenge. Could you please explain the following for me: (1) The question reads to not loop over a range of values using a For or While Loop? (2) I did not had to worry over the default case since it already has been taking care of by the Return Number? (3) Why would we use "i"

Please, advise

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi Michael -

"I" is just a placeholder so that you can iterate through the values

Here is another example that may assist you in understanding, if you run in your playground you will see that it prints the colors because it is iterating through them

var colors = ["red","blue", "orange"]

for color in colors{
    print(color)
}
Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

Thank you Steve!

The For/In loop example you provided makes sense to me as well as the "i" placeholder. I would assume you could choose any letter - but "i" of "integer" makes more sense.

Yet, when copy past your first feedback into the code challenge editor I receive the following message "Double check your logic for Fizz values and make sure you're returning the correct string!"

Not sure what is incorrect. Please, advise