Skip to content

Instantly share code, notes, and snippets.

@davidhariri
Created March 12, 2017 15:41
Show Gist options
  • Save davidhariri/7412ef416708ab64225e711160ce867b to your computer and use it in GitHub Desktop.
Save davidhariri/7412ef416708ab64225e711160ce867b to your computer and use it in GitHub Desktop.
DRY-er FizzBuzz Solution in Swift
// Dry FizzBuzz solution for Swift 3 by David Hariri
// Made it for the Treehouse Swift Basics class
import Foundation
func isMultipleOf(n: Int, m: Int) -> Bool {
return n % m == 0
}
func fizzBuzz(n: Int) -> String {
var res: String = ""
let mults: [Int: String] = [
3: "Fizz",
5: "Buzz"
]
for x:Int in [3, 5] {
if isMultipleOf(n: n, m: x) {
res += mults[x]!
}
}
if res == "" {
return "\(n)"
}
return res
}
for n in 1...100 {
print(fizzBuzz(n: n))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment