mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
95 Workday - introduce some comments, and methods; improve some variable names; spotted a bug
This commit is contained in:
@@ -46,7 +46,33 @@ function fnb(arg) {
|
|||||||
return Math.floor(arg / 7);
|
return Math.floor(arg / 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
const t = [, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5];
|
// in a non-leap year the day of the week for the first of each month moves by the following amounts.
|
||||||
|
const MONTHLY_DAY_OF_WEEK_OFFSETS = [0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a date, and extracts the date information.
|
||||||
|
* This expects date parts to be comma separated, using US date ordering,
|
||||||
|
* i.e. Month,Day,Year.
|
||||||
|
* @returns {Promise<[number,number,number]>} [year, month, dayOfMonth]
|
||||||
|
*/
|
||||||
|
async function readDateElements() {
|
||||||
|
let dateString = await input();
|
||||||
|
const month = parseInt(dateString);
|
||||||
|
const dayOfMonth = parseInt(dateString.substr(dateString.indexOf(",") + 1));
|
||||||
|
const year = parseInt(dateString.substr(dateString.lastIndexOf(",") + 1));
|
||||||
|
return [year, month, dayOfMonth];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a US formatted date, i.e. Month/Day/Year.
|
||||||
|
* @param year
|
||||||
|
* @param month
|
||||||
|
* @param dayOfMonth
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function getFormattedDate(year, month, dayOfMonth) {
|
||||||
|
return month + "/" + dayOfMonth + "/" + year;
|
||||||
|
}
|
||||||
|
|
||||||
let k5;
|
let k5;
|
||||||
let k6;
|
let k6;
|
||||||
@@ -73,6 +99,46 @@ function time_spent(f, a8)
|
|||||||
print(i5 + "\t" + i6 + "\t" + i7 + "\n");
|
print(i5 + "\t" + i6 + "\t" + i7 + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getDayOfWeek(dobYear, dobMonth, dobDayOfMonth) {
|
||||||
|
const i1 = Math.floor((dobYear - 1500) / 100);
|
||||||
|
let a = i1 * 5 + (i1 + 3) / 4;
|
||||||
|
const i2 = Math.floor(a - fnb(a) * 7);
|
||||||
|
const y2 = Math.floor(dobYear / 100);
|
||||||
|
const y3 = Math.floor(dobYear - y2 * 100);
|
||||||
|
a = y3 / 4 + y3 + dobDayOfMonth + MONTHLY_DAY_OF_WEEK_OFFSETS[dobMonth-1] + i2;
|
||||||
|
let dayOfWeek = Math.floor(a - fnb(a) * 7) + 1;
|
||||||
|
if (dobMonth <= 2) {
|
||||||
|
if (y3 !== 0) {
|
||||||
|
t1 = Math.floor(dobYear - fna(dobYear) * 4);
|
||||||
|
} else {
|
||||||
|
a = i1 - 1;
|
||||||
|
t1 = Math.floor(a - fna(a) * 4);
|
||||||
|
}
|
||||||
|
if (t1 === 0) {
|
||||||
|
if (dayOfWeek === 0) {
|
||||||
|
dayOfWeek = 6;
|
||||||
|
}
|
||||||
|
dayOfWeek--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dayOfWeek === 0) {
|
||||||
|
dayOfWeek = 7;
|
||||||
|
}
|
||||||
|
return dayOfWeek;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The following performs a special hash on the day parts which guarantees
|
||||||
|
* that different days will return different numbers, and the numbers returned are in ordered.
|
||||||
|
* @param todayYear
|
||||||
|
* @param todayMonth
|
||||||
|
* @param todayDayOfMonth
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
function getNormalisedDay(todayYear, todayMonth, todayDayOfMonth) {
|
||||||
|
return (todayYear * 12 + todayMonth) * 31 + todayDayOfMonth;
|
||||||
|
}
|
||||||
|
|
||||||
// Main control section
|
// Main control section
|
||||||
async function main()
|
async function main()
|
||||||
{
|
{
|
||||||
@@ -85,59 +151,38 @@ async function main()
|
|||||||
print("GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.\n");
|
print("GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.\n");
|
||||||
print("\n");
|
print("\n");
|
||||||
print("ENTER TODAY'S DATE IN THE FORM: 3,24,1979 ");
|
print("ENTER TODAY'S DATE IN THE FORM: 3,24,1979 ");
|
||||||
let str = await input();
|
const [todayYear, todayMonth, todayDayOfMonth] = await readDateElements();
|
||||||
const m1 = parseInt(str);
|
|
||||||
const d1 = parseInt(str.substr(str.indexOf(",") + 1));
|
|
||||||
const y1 = parseInt(str.substr(str.lastIndexOf(",") + 1));
|
|
||||||
// This program determines the day of the week
|
// This program determines the day of the week
|
||||||
// for a date after 1582
|
// for a date after 1582
|
||||||
print("ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST)");
|
print("ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST)");
|
||||||
str = await input();
|
const [dobYear, dobMonth, dobDayOfMonth] = await readDateElements();
|
||||||
const m = parseInt(str);
|
|
||||||
const d = parseInt(str.substr(str.indexOf(",") + 1));
|
|
||||||
const y = parseInt(str.substr(str.lastIndexOf(",") + 1));
|
|
||||||
print("\n");
|
print("\n");
|
||||||
const i1 = Math.floor((y - 1500) / 100);
|
|
||||||
// Test for date before current calendar.
|
// Test for date before current calendar.
|
||||||
if (y - 1582 < 0) {
|
// Note: this test is unreliable - the Gregorian calendar was introduced on Friday 15 October 1582
|
||||||
|
// and the weekday algorithm fails for dates prior to that
|
||||||
|
if (dobYear - 1582 < 0) {
|
||||||
print("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII.\n");
|
print("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII.\n");
|
||||||
} else {
|
} else {
|
||||||
let a = i1 * 5 + (i1 + 3) / 4;
|
const dayOfWeek = getDayOfWeek(dobYear, dobMonth, dobDayOfMonth);
|
||||||
const i2 = Math.floor(a - fnb(a) * 7);
|
|
||||||
const y2 = Math.floor(y / 100);
|
const normalisedToday = getNormalisedDay(todayYear, todayMonth, todayDayOfMonth);
|
||||||
const y3 = Math.floor(y - y2 * 100);
|
const normalisedDob = getNormalisedDay(dobYear, dobMonth, dobDayOfMonth);
|
||||||
a = y3 / 4 + y3 + d + t[m] + i2;
|
|
||||||
let b = Math.floor(a - fnb(a) * 7) + 1;
|
if (normalisedToday < normalisedDob) {
|
||||||
if (m <= 2) {
|
print(getFormattedDate(dobYear, dobMonth, dobDayOfMonth) + " WILL BE A ");
|
||||||
if (y3 !== 0) {
|
} else if (normalisedToday === normalisedDob) {
|
||||||
t1 = Math.floor(y - fna(y) * 4);
|
print(getFormattedDate(dobYear, dobMonth, dobDayOfMonth) + " IS A ");
|
||||||
} else {
|
|
||||||
a = i1 - 1;
|
|
||||||
t1 = Math.floor(a - fna(a) * 4);
|
|
||||||
}
|
|
||||||
if (t1 === 0) {
|
|
||||||
if (b === 0)
|
|
||||||
b = 6;
|
|
||||||
b--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (b === 0)
|
|
||||||
b = 7;
|
|
||||||
if ((y1 * 12 + m1) * 31 + d1 < (y * 12 + m) * 31 + d) {
|
|
||||||
print(m + "/" + d + "/" + y + " WILL BE A ");
|
|
||||||
} else if ((y1 * 12 + m1) * 31 + d1 === (y * 12 + m) * 31 + d) {
|
|
||||||
print(m + "/" + d + "/" + y + " IS A ");
|
|
||||||
} else {
|
} else {
|
||||||
print(m + "/" + d + "/" + y + " WAS A ");
|
print(getFormattedDate(dobYear, dobMonth, dobDayOfMonth) + " WAS A ");
|
||||||
}
|
}
|
||||||
switch (b) {
|
switch (dayOfWeek) {
|
||||||
case 1: print("SUNDAY.\n"); break;
|
case 1: print("SUNDAY.\n"); break;
|
||||||
case 2: print("MONDAY.\n"); break;
|
case 2: print("MONDAY.\n"); break;
|
||||||
case 3: print("TUESDAY.\n"); break;
|
case 3: print("TUESDAY.\n"); break;
|
||||||
case 4: print("WEDNESDAY.\n"); break;
|
case 4: print("WEDNESDAY.\n"); break;
|
||||||
case 5: print("THURSDAY.\n"); break;
|
case 5: print("THURSDAY.\n"); break;
|
||||||
case 6:
|
case 6:
|
||||||
if (d === 13) {
|
if (dobDayOfMonth === 13) {
|
||||||
print("FRIDAY THE THIRTEENTH---BEWARE!\n");
|
print("FRIDAY THE THIRTEENTH---BEWARE!\n");
|
||||||
} else {
|
} else {
|
||||||
print("FRIDAY.\n");
|
print("FRIDAY.\n");
|
||||||
@@ -145,31 +190,32 @@ async function main()
|
|||||||
break;
|
break;
|
||||||
case 7: print("SATURDAY.\n"); break;
|
case 7: print("SATURDAY.\n"); break;
|
||||||
}
|
}
|
||||||
if ((y1 * 12 + m1) * 31 + d1 !== (y * 12 + m) * 31 + d) {
|
if (normalisedToday !== normalisedDob) {
|
||||||
let i5 = y1 - y;
|
let yearsBetweenDates = todayYear - dobYear;
|
||||||
print("\n");
|
print("\n");
|
||||||
let i6 = m1 - m;
|
let monthsBetweenDates = todayMonth - dobMonth;
|
||||||
let i7 = d1 - d;
|
let daysBetweenDates = todayDayOfMonth - dobDayOfMonth;
|
||||||
if (i7 < 0) {
|
if (daysBetweenDates < 0) {
|
||||||
i6--;
|
monthsBetweenDates--;
|
||||||
i7 += 30;
|
daysBetweenDates += 30;
|
||||||
}
|
}
|
||||||
if (i6 < 0) {
|
if (monthsBetweenDates < 0) {
|
||||||
i5--;
|
yearsBetweenDates--;
|
||||||
i6 += 12;
|
monthsBetweenDates += 12;
|
||||||
}
|
}
|
||||||
if (i5 >= 0) {
|
if (yearsBetweenDates >= 0) {
|
||||||
if (i7 === 0 && i6 === 0)
|
if (daysBetweenDates === 0 && monthsBetweenDates === 0) {
|
||||||
print("***HAPPY BIRTHDAY***\n");
|
print("***HAPPY BIRTHDAY***\n");
|
||||||
|
}
|
||||||
print(" \tYEARS\tMONTHS\tDAYS\n");
|
print(" \tYEARS\tMONTHS\tDAYS\n");
|
||||||
print(" \t-----\t------\t----\n");
|
print(" \t-----\t------\t----\n");
|
||||||
print("YOUR AGE (IF BIRTHDATE) \t" + i5 + "\t" + i6 + "\t" + i7 + "\n");
|
print("YOUR AGE (IF BIRTHDATE) \t" + yearsBetweenDates + "\t" + monthsBetweenDates + "\t" + daysBetweenDates + "\n");
|
||||||
const a8 = (i5 * 365) + (i6 * 30) + i7 + Math.floor(i6 / 2);
|
const a8 = (yearsBetweenDates * 365) + (monthsBetweenDates * 30) + daysBetweenDates + Math.floor(monthsBetweenDates / 2);
|
||||||
k5 = i5;
|
k5 = yearsBetweenDates;
|
||||||
k6 = i6;
|
k6 = monthsBetweenDates;
|
||||||
k7 = i7;
|
k7 = daysBetweenDates;
|
||||||
// Calculate retirement date.
|
// Calculate retirement date.
|
||||||
const e = y + 65;
|
const e = dobYear + 65;
|
||||||
// Calculate time spent in the following functions.
|
// Calculate time spent in the following functions.
|
||||||
print("YOU HAVE SLEPT \t\t\t");
|
print("YOU HAVE SLEPT \t\t\t");
|
||||||
time_spent(0.35, a8);
|
time_spent(0.35, a8);
|
||||||
|
|||||||
Reference in New Issue
Block a user