Skip to content

Commit c324ac1

Browse files
committed
feat(lesson07): add largest number, palindrome, and fibonacci functions
1 parent 79ac727 commit c324ac1

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

lesson_07/conditionals/src/part_g.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,50 @@ export function isPalindrome(text: string): boolean {
4040
}
4141

4242
/**
43-
* Calculates the number of days until the next birthday.
44-
* Assume currentMonth and currentDay represent today's date,
45-
* and birthMonth and birthDay represent the birthday.
43+
* Returns an array of the first `n` Fibonacci numbers starting from 1.
4644
*
47-
* @param currentMonth (1-12)
48-
* @param currentDay (1-31)
49-
* @param birthMonth (1-12)
50-
* @param birthDay (1-31)
51-
* @returns
45+
* @param n The first `n` of Fibonacci values to compute.
46+
* @return An array containing the first `n` Fibonacci values.
5247
*/
48+
export function getFirstNFibonacciNumbers(n: number): number[] {
49+
if (n <= 0) return []; // if n is 0 or negative → return empty
50+
if (n === 1) return [1]; // if n is 1 → just [1]
51+
52+
const fib: number[] = [1, 1]; // start with 1, 1
53+
54+
// keep making new numbers until we reach n
55+
for (let i = 2; i < n; i++) {
56+
const next = fib[i - 1] + fib[i - 2]; // add the last two numbers
57+
fib.push(next); // put the new number in the list
58+
}
59+
60+
return fib; // give back the whole list
61+
}
62+
63+
64+
5365
export function daysUntilBirthday(
5466
currentMonth: number,
5567
currentDay: number,
5668
birthMonth: number,
5769
birthDay: number
5870
): number {
59-
throw new Error("Not implemented yet");
71+
// Step 1: Get today's year
72+
const todayYear = new Date().getFullYear();
73+
74+
// Step 2: Create Date objects for "today" and "next birthday"
75+
const today = new Date(todayYear, currentMonth - 1, currentDay);
76+
let nextBirthday = new Date(todayYear, birthMonth - 1, birthDay);
77+
78+
// Step 3: If birthday already passed this year, use next year
79+
if (nextBirthday < today) {
80+
nextBirthday = new Date(todayYear + 1, birthMonth - 1, birthDay);
81+
}
82+
83+
// Step 4: Calculate difference in milliseconds, convert to days
84+
const diffMs = nextBirthday.getTime() - today.getTime();
85+
const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24));
86+
87+
return diffDays;
6088
}
89+

0 commit comments

Comments
 (0)