Skip to content

Instantly share code, notes, and snippets.

@OmarMakled
Last active May 24, 2017 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OmarMakled/12fe20d8b595a823f638161566af16a5 to your computer and use it in GitHub Desktop.
Save OmarMakled/12fe20d8b595a823f638161566af16a5 to your computer and use it in GitHub Desktop.
function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers
if (! function_exists('isEvenOdd')) {
/**
* Determine if given number even or odd
*
* @param float|int $number
*
* @return string|InvalidArgumentException
*/
function isEvenOdd($number)
{
if (! is_numeric($number)) {
throw new InvalidArgumentException("Giving value must be a numeric {$number}", 100);
}
return ($number % 2 == 0)
? 'Even'
: 'Odd';
}
}
/**
* @expectedException InvalidArgumentException
*/
public function test_is_even_odd()
{
foreach ([-2, 0, 2, 4, 6] as $number) {
$this->assertEquals(isEvenOdd($number), 'Even');
}
foreach ([-1, 1, 3, 5, 7] as $number) {
$this->assertEquals(isEvenOdd($number), 'Odd');
}
// InvalidArgumentException
isEvenOdd('foo');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment