Skip to content

Instantly share code, notes, and snippets.

@dkliman
Last active July 1, 2017 01:45
Show Gist options
  • Save dkliman/4a32b49b586c154e480479f1f96b9963 to your computer and use it in GitHub Desktop.
Save dkliman/4a32b49b586c154e480479f1f96b9963 to your computer and use it in GitHub Desktop.
Swift Coding Challenges Chapter 2, Challenge 16: Fizz Buzz
//Chapter 2: Numbers
//Challenge 16: Fizz Buzz
func fizzBuzz() {
for number in 1...100 {
let fizz = (number % 3 == 0) ? "Fizz " : ""
let buzz = (number % 5 == 0) ? "Buzz" : ""
let printableNumber = fizz + buzz == "" ? String(number) : ""
print ("\(printableNumber)\(fizz)\(buzz)")
}
}
fizzBuzz()
@dkliman
Copy link
Author

dkliman commented Jun 30, 2017

now only prints numbers when they are divisible by neither 3 nor 5.

@dkliman
Copy link
Author

dkliman commented Jul 1, 2017

now correctly formats "Fizz Buzz" when both appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment