I went to a job interview last Thursday in which I felt I did well except
for one question that he asked me. He asked me to create a PHP function that
takes an input and outputs even if a number is even and odd if a number is
odd.* Below are the function that I wrote in the interview, and the function
that I would have liked to have written in the interview. Now here are some
questions for you:
1) is there a big difference in the two functions?
2) if you could grade these on completeness, how would you score them?
3) what would be your perfect answer?
*Note: this wasn't a job exactly for PHP, but does require some coding
abilities. My resume said I knew PHP
I wrote a function like this:
function evenOrOdd ($number) {
if ($number % 2 == 0) {
echo 'even';
} else {
echo 'odd';
}
}
This is what I would have liked to have written:
/* This function determines whether an inputted number is even or odd
according
to the definition on wiki
http://en.wikipedia.org/wiki/Even_and_odd_numbers
viewed 8 Oct 2008. */
function evenOrOdd ($number) {
if (is_int($number)) {
if (($number % 2) == 0) {
echo 'Your number ' . $number . ' is even.';
} else if (($number % 2) == 1 || ($number % 2) == -1) {
echo 'Your number ' . $number . ' is odd.';
}
} else {
echo 'Your input ' . $number . ' is not an integer.';
}
}
_______________________________________________
UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net