Skip to content

Commit c7edc9d

Browse files
dxbenson252anthonydmays
authored andcommitted
Completed: Loops for functions & Part_G Assmt
1 parent 0a69ac4 commit c7edc9d

File tree

3 files changed

+82
-37
lines changed

3 files changed

+82
-37
lines changed

lesson_07/conditionals/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
HW_VERSION=your homework version here
1+
HW_VERSION=G

lesson_07/conditionals/src/lesson7.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ export function compareStrings(a: string, b: string): number {
1313
const distance = computeLexicographicDistance(a, b);
1414

1515
// TODO(you): Finish this method.
16-
17-
return 0;
16+
if (distance < 0) {
17+
return -1;
18+
} else if (distance > 0) {
19+
return 1;
20+
} else {
21+
return 0;
22+
}
1823
}
1924

2025
/**
@@ -24,7 +29,15 @@ export function compareStrings(a: string, b: string): number {
2429
* @return The factorial of n.
2530
*/
2631
export function computeFactorial(n: number): number {
27-
return 0;
32+
if (n <= 0) {
33+
return 1; // By convention, 0! = 1
34+
}
35+
36+
let result = 1;
37+
for (let i = 1; i <= n; i++) {
38+
result *= i;
39+
}
40+
return result;
2841
}
2942

3043
/**
@@ -34,7 +47,24 @@ export function computeFactorial(n: number): number {
3447
* @return An array containing the first `n` Fibonacci values.
3548
*/
3649
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
50+
if (n <= 0) {
51+
return [];
52+
}
53+
54+
if (n === 1) {
55+
return [1];
56+
}
57+
58+
if (n === 2) {
59+
return [1, 1];
60+
}
61+
62+
const fibonacci = [1, 1];
63+
for (let i = 2; i < n; i++) {
64+
fibonacci.push(fibonacci[i - 1] + fibonacci[i - 2]);
65+
}
66+
67+
return fibonacci;
3868
}
3969

4070
/**
@@ -65,5 +95,12 @@ export function binarySearch(
6595
// Else if values[pivotIndex] is greater than the value, then
6696
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
6797
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
68-
return -1;
98+
99+
if (values[pivotIndex] === value) {
100+
return pivotIndex;
101+
} else if (values[pivotIndex] > value) {
102+
return binarySearch(values, start, pivotIndex - 1, value);
103+
} else {
104+
return binarySearch(values, pivotIndex + 1, end, value);
105+
}
69106
}
Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
1-
/**
2-
* Finds the largest number in an array.
3-
*
4-
* @param numbers
5-
* @returns
6-
*/
71
export function findLargestNumber(numbers: number[]): number {
8-
return 0;
2+
if (numbers.length === 0) return 0;
3+
4+
let largest = numbers[0];
5+
for (let i = 1; i < numbers.length; i++) {
6+
if (numbers[i] > largest) {
7+
largest = numbers[i];
8+
}
9+
}
10+
return largest;
911
}
1012

11-
/**
12-
* Determines if a string is a palindrome (reads the same forwards and backwards).
13-
* Ignore case and spaces.
14-
*
15-
* @param text
16-
* @returns
17-
*/
18-
export function isPalindrome(text: string): boolean {
19-
return false;
20-
}
2113

22-
/**
23-
* Calculates the number of days until the next birthday.
24-
* Assume currentMonth and currentDay represent today's date,
25-
* and birthMonth and birthDay represent the birthday.
26-
*
27-
* @param currentMonth (1-12)
28-
* @param currentDay (1-31)
29-
* @param birthMonth (1-12)
30-
* @param birthDay (1-31)
31-
* @returns
32-
*/
33-
export function daysUntilBirthday(
14+
export function isPalindrome(text: string): boolean {
15+
const cleanText = text.replace(/\s/g, "").toLowerCase();
16+
17+
for (let i = 0; i < cleanText.length / 2; i++) {
18+
if (cleanText[i] !== cleanText[cleanText.length - 1 - i]) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
25+
export function daysUntilBirthday(
3426
currentMonth: number,
3527
currentDay: number,
3628
birthMonth: number,
3729
birthDay: number
3830
): number {
39-
return 0;
40-
}
31+
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
32+
33+
const dayOfYear = (m: number, d: number): number => {
34+
let sum = 0;
35+
for (let i = 0; i < m - 1; i++) sum += daysInMonth[i];
36+
return sum + d;
37+
};
38+
39+
const cur = dayOfYear(currentMonth, currentDay);
40+
const bday = dayOfYear(birthMonth, birthDay);
41+
42+
if (cur === bday) return 0; // Today is the birthday
43+
44+
let diff = bday - cur;
45+
if (diff < 0) diff += 365; // Birthday is next year
46+
47+
return diff;
48+
}

0 commit comments

Comments
 (0)