Truth tables - Yolk Recruitment

TRUTH TABLES
function getNumberOfDaysInMonth($month) {
for ($i = 0; $i <= 32; $i++) {
// errr...
}
}
ATTEMPT #1
function getNumberOfDaysInMonth($month) {
$months = [‘Jan’, ‘Feb’, ‘Mar’];
$days
= [31,
// errr...
}
ATTEMPT #2
28,
31];
function getNumberOfDaysInMonth($month) {
$daysInMonths = [
‘Jan’ => 31,
‘Feb’ => 28,
‘Mar’ => 31,
];
return $daysInMonths[$month];
}
ATTEMPT #3
if ($year % 4 == 0) {
if ($year % 100) // got stuck here
}
ATTEMPT #4
if ($year % 4 == 0) {
if ($year % 100 == 0) {
if ($year % 400 == 0) {
return true;
}
return false;
}
return true;
}
LOGICAL RESULT
Is Divisible
By 4?




Is Divisible
By 100?
Is Divisible
By 400?





LEAP YEAR TRUTH TABLE
Is Leap
Year?




function isLeapYear($year) {
return $year % 4 == 0
&& ($year % 100 != 0 || $year % 400 == 0);
}
J
a
n
F M A M J
e a p a u
b r r y n
J
u
l
A S O N D
u e c o e
g p t v v

28/29




30



 


31
DAYS OF THE MONTH TRUTH TABLE
function getNumDaysInMonth($month, $year) {
if ($month == ‘Feb’) {
return isLeapYear($year) ? 29 : 28;
}
if (in_array($month, [‘Sep’, ‘Apr’, ‘Jun’, ‘Nov’])) {
return 30;
}
return 31;
}
function getNumDaysInMonth($month, $year) {
if ($month == ‘Feb’) {
return isLeapYear($year) ? 29 : 28;
}
return in_array($month, [‘Sep’, ‘Apr’, ‘Jun’, ‘Nov’])
? 30
: 31;
}
function getNumDaysInMonth($month, $year = null) {
if ($month == ‘Feb’) {
return $year && isLeapYear($year) ? 29 : 28;
}
return in_array($month, [‘Sep’, ‘Apr’, ‘Jun’, ‘Nov’])
? 30
: 31;
}