Created
June 30, 2018 20:23
-
-
Save thiskevinwang/9b5d5ee2ead28c6adef900bd184f68a4 to your computer and use it in GitHub Desktop.
[CodeWars] evenOrOdd https://www.codewars.com/kata/53da3dbb4a5168369a0000fe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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