Skip to content

Instantly share code, notes, and snippets.

@maxwellimpact
Created March 8, 2015 21:41
Show Gist options
  • Save maxwellimpact/710aacd9248ad9cba5f8 to your computer and use it in GitHub Desktop.
Save maxwellimpact/710aacd9248ad9cba5f8 to your computer and use it in GitHub Desktop.
import UIKit
// lame FizzBuzz test in swift
// 1
for x in 1...100 {
if x % 3 == 0 && x % 5 == 0 {
println("FizzBuzz")
} else if x % 3 == 0 {
println("Fizz")
} else if x % 5 == 0 {
println("Buzz")
} else {
println(x)
}
}
// 2
for x in 1...100 {
var message = ""
if x % 3 == 0 {
message += "Fizz"
}
if x % 5 == 0 {
message += "Buzz"
}
println(!message.isEmpty ? message : "\(x)")
}
// 3
for x in 1...100 {
switch x {
case x where x % 3 == 0 && x % 5 == 0:
println("FizzBuzz")
case x where x % 3 == 0:
println("Fizz")
case x where x % 5 == 0:
println("Buzz")
default:
println("\(x)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment