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 Basics (retired) Control Flow Exercise: FizzBuzz Generator

Samar Khanna
Samar Khanna
1,757 Points

Is my solution fine? I created a variable before this.

var gameNumber = 1...30

for inputNumber in gameNumber { if (inputNumber % 3) == 0 && (inputNumber % 5) == 0 { println("FizzBuzz") } else if (inputNumber % 3) == 0 { println("Fizz") } else if (inputNumber % 5) == 0 { println("Buzz") } else { println(inputNumber) } }

I have created a game Number variable (or array i am not sure) and then written code for the game. Is what I have done seen as good practice and is it advisable for me to carry on with this code? It works, but is what Amit did seen as better practice?

Philip Ondrejack
Philip Ondrejack
4,287 Points

Check out the markdown sheet so you're forum questions are more readable. Code in a post should look like this for readability:

func greeting(person: String) -> (greeting: String, language: String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (greeting,language)
}

var result = greeting("Tom")

1 Answer

It looks fine to me... but if you want you could me make more simple just by 1...30 into the loop

   for inputNumber in 1...30 {
     if (inputNumber % 3) == 0 && (inputNumber % 5) == 0
    {
     println("FizzBuzz")
    }
   else if (inputNumber % 3) == 0
  {
    println("Fizz")
   }
  else if (inputNumber % 5) == 0
  { 
    println("Buzz")
  } 
  else
 { println(inputNumber)
 }
}
Samar Khanna
Samar Khanna
1,757 Points

So i can use a separate variable right? i mean there is nothing like Amit's answer is more suitable?