Skip to content

Instantly share code, notes, and snippets.

@thiskevinwang
Created June 30, 2018 20:23
Show Gist options
  • Save thiskevinwang/9b5d5ee2ead28c6adef900bd184f68a4 to your computer and use it in GitHub Desktop.
Save thiskevinwang/9b5d5ee2ead28c6adef900bd184f68a4 to your computer and use it in GitHub Desktop.
// Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
// 1. via "if else" statements
func evenOrOdd(_ number: Int) -> String {
if number % 2 == 0 {
return "Even"
} else {
return "Odd"
}
}
// 2. via "ternary operator"
func evenOrOdd(_ number:Int) -> String {
return number % 2 == 0 ? "Even" : "Odd"
}
// 3. via "guard" statement
func evenOrOdd(_ number:Int) -> String {
guard number % 2 == 0 else { return "Odd" }
return "Even"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment