Created
June 11, 2013 13:45
-
-
Save jasonrobertfox/5756980 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
//Input Data | |
$name = "Jason"; | |
$age = 28; | |
function getPlural($ageArgument){ | |
if($ageArgument == 1){ | |
return ""; | |
} else { | |
return "s"; | |
} | |
} | |
function getOneToNine($age){ | |
$numbers = array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); | |
//subtract 1 to shift the index | |
return $numbers[$age-1]; | |
} | |
function getStrangeNumbers($age){ | |
$numbers = array("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"); | |
//subtract 10 to shift the index | |
return $numbers[$age-10]; | |
} | |
function getAboveTwenty($age){ | |
//split the age into an array of first and second digit | |
$parsed = str_split($age); | |
//create the other words | |
$tens = array("twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"); | |
//the first word is based on the first digit, minus "2" to account for the index | |
$firstWord = $tens[$parsed[0] - 2]; | |
//in other words, give me the first element of $parsed, subtract 2, then give me that element of $tens | |
//if the second number is a zero we can just return the first word, otherwise we have to get the second number | |
if ($parsed[1] == 0){ | |
return $firstWord; | |
} else { | |
//otherwise lets use our existing code to get the second word: | |
return $firstWord . " " . getOneToNine($parsed[1]); | |
//so we join the first word with a space to the result of getting 1 to 9 | |
} | |
} | |
function numberLessThanOneHundredToText($age){ | |
//using <= to prevent negative ages, which are silly | |
if($age <= 0){ | |
return "zero"; | |
}else if ($age < 10){ | |
return getOneToNine($age); | |
}else if ($age < 20){ | |
return getStrangeNumbers($age); | |
}else{ | |
return getAboveTwenty($age); | |
} | |
} | |
function changeNumberToText($age){ | |
//using >= here to prevent numbers above 100, which is old. | |
if($age >= 100){ | |
return "one hundred"; | |
} else { | |
return numberLessThanOneHundredToText($age); | |
} | |
} | |
$plural = getPlural($age); | |
$newAge = changeNumberToText($age); | |
//Final Output | |
echo "Happy Birthday $name, you are $newAge year$plural old!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment