11import textContent from "./contents.json" ;
22
3+ /**
4+ * Checks if the deathdate is empty or undefined
5+ * @param deathdate the deathdate of the cat
6+ */
7+ function isAlive ( deathdate : string ) : boolean {
8+ return deathdate === "" || deathdate === undefined ;
9+ }
10+
311/**
412 * This takes in the entire props structure and manipulates incoming string data to generate
513 * a formatted output to be directly displayed.
@@ -9,19 +17,27 @@ export function nextBirthdayAsStr(): string {
917 let soonestName : string = ""
1018 let soonestDays : number = 99999
1119 textContent . cards . forEach ( details => {
12- console . log ( details . deathdate ) ;
13- if ( details . deathdate === "" || details . deathdate === undefined ) {
14- const date = new Date ( details . birthdate ) ;
20+ // Difference between todays date and birthdate
21+ let diffDays ;
22+
23+ if ( isAlive ( details . deathdate ) ) {
24+ const birthDayDate = new Date ( details . birthdate ) ;
1525 const todaysDate = new Date ( ) ;
1626
17- date . setFullYear ( 2000 ) ;
18- todaysDate . setFullYear ( 2000 ) ;
27+ // Assign birthday to the current year to test next birthday
28+ birthDayDate . setFullYear ( todaysDate . getFullYear ( ) ) ;
1929
20- let diff = date . getTime ( ) - todaysDate . getTime ( )
21- var diffDays = Math . ceil ( diff / ( 1000 * 3600 * 24 ) ) ;
30+ const dateDiff = birthDayDate . getTime ( ) - todaysDate . getTime ( )
31+ if ( dateDiff < 0 ) {
32+ // Birthday has already passed so we recalculate for next year
33+ birthDayDate . setFullYear ( todaysDate . getFullYear ( ) + 1 ) ;
34+ const dateDiffNextYear = birthDayDate . getTime ( ) - todaysDate . getTime ( )
35+ diffDays = Math . ceil ( dateDiffNextYear / ( 1000 * 3600 * 24 ) ) ;
36+ } else {
37+ diffDays = Math . ceil ( dateDiff / ( 1000 * 3600 * 24 ) ) ;
38+ }
2239
23- // Number of diff days is in the future, and is less than current soonest days
24- if ( diffDays > 0 && diffDays < soonestDays ) {
40+ if ( diffDays < soonestDays ) {
2541 soonestName = details . name
2642 soonestDays = diffDays
2743 }
0 commit comments