Skip to content

Commit 139ff82

Browse files
committed
chore: adds functions for Lesson7 and part_d
1 parent c668fa2 commit 139ff82

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import { computeLexicographicDistance } from "./util.js";
88
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
99
*/
1010
export function compareStrings(a: string, b: string): number {
11-
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
12-
// if it is greater, and 0 if the strings are equal.
1311
const distance = computeLexicographicDistance(a, b);
1412

15-
// TODO(you): Finish this method.
16-
17-
return 0;
13+
if (distance < 0) {
14+
return -1;
15+
} else if (distance > 0) {
16+
return 1;
17+
} else {
18+
return 0;
19+
}
1820
}
1921

2022
/**
@@ -24,7 +26,16 @@ export function compareStrings(a: string, b: string): number {
2426
* @return The factorial of n.
2527
*/
2628
export function computeFactorial(n: number): number {
27-
return 0;
29+
if (n === 0) {
30+
return 1;
31+
}
32+
33+
let result = 1;
34+
for (let i = 1; i <= n; i++) {
35+
result *= i;
36+
}
37+
38+
return result;
2839
}
2940

3041
/**
@@ -34,7 +45,20 @@ export function computeFactorial(n: number): number {
3445
* @return An array containing the first `n` Fibonacci values.
3546
*/
3647
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
48+
if (n <= 0) return [];
49+
50+
const fib: number[] = [1];
51+
52+
if (n === 1) return fib;
53+
54+
fib.push(1); // Add the second 1
55+
56+
for (let i = 2; i < n; i++) {
57+
const next = fib[i - 1] + fib[i - 2];
58+
fib.push(next);
59+
}
60+
61+
return fib;
3862
}
3963

4064
/**
@@ -53,17 +77,16 @@ export function binarySearch(
5377
value: number,
5478
): number {
5579
if (end < start) {
56-
// The range is not valid so just return -1.
57-
return -1;
80+
return -1; // Base case: value not found
5881
}
5982

60-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
83+
const pivotIndex = Math.floor((start + end) / 2);
6184

62-
// TODO(you): Finish implementing this algorithm
63-
64-
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65-
// Else if values[pivotIndex] is greater than the value, then
66-
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
67-
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
68-
return -1;
85+
if (values[pivotIndex] === value) {
86+
return pivotIndex;
87+
} else if (values[pivotIndex] > value) {
88+
return binarySearch(values, start, pivotIndex - 1, value);
89+
} else {
90+
return binarySearch(values, pivotIndex + 1, end, value);
91+
}
6992
}

lesson_07/conditionals/src/part_d.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* @returns
88
*/
99
export function isWithinRange(num: number, min: number, max: number): boolean {
10-
return false;
10+
return num >= min && num <= max;
1111
}
1212

13+
console.log(isWithinRange(7, 5, 10));
14+
console.log(isWithinRange(2, 5, 10));
15+
1316
/**
1417
* Determine if a shape is a triangle based on the given side lengths.
1518
*
@@ -19,7 +22,7 @@ export function isWithinRange(num: number, min: number, max: number): boolean {
1922
* @returns
2023
*/
2124
export function isValidTriangle(a: number, b: number, c: number): boolean {
22-
return false;
25+
return a + b > c && a + c > b && b + c > a;
2326
}
2427

2528
/**
@@ -30,5 +33,15 @@ export function isValidTriangle(a: number, b: number, c: number): boolean {
3033
* @returns
3134
*/
3235
export function getSeason(month: number): string {
33-
return "Invalid month";
34-
}
36+
if (month >= 3 && month <= 5) {
37+
return "Spring";
38+
} else if (month >= 6 && month <= 8) {
39+
return "Summer";
40+
} else if (month >= 9 && month <= 11) {
41+
return "Fall";
42+
} else if (month === 12 || month === 1 || month === 2) {
43+
return "Winter";
44+
} else {
45+
return "Invalid month";
46+
}
47+
}

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)