Skip to content

Commit e4204bf

Browse files
committed
feat: add completed lesson7.ts file
1 parent 888672a commit e4204bf

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ export function compareStrings(a: string, b: string): number {
1414

1515
// TODO(you): Finish this method.
1616

17-
return 0;
17+
if (distance < 0) { // If the distance is less than 0, then 'a' is less than 'b'
18+
return -1;
19+
} else if (distance > 0) {
20+
return 1;
21+
} else {
22+
return 0;
23+
}
1824
}
1925

2026
/**
@@ -24,7 +30,18 @@ export function compareStrings(a: string, b: string): number {
2430
* @return The factorial of n.
2531
*/
2632
export function computeFactorial(n: number): number {
27-
return 0;
33+
if (n === 0) {
34+
return 1;
35+
}
36+
if (n < 0) {
37+
return 0;
38+
}
39+
let result = 1;
40+
for (let i = 1; i <= n; i++) {
41+
result = result * i;
42+
}
43+
44+
return result; // Return the final result.
2845
}
2946

3047
/**
@@ -34,7 +51,18 @@ export function computeFactorial(n: number): number {
3451
* @return An array containing the first `n` Fibonacci values.
3552
*/
3653
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
54+
if (n <= 0) {
55+
return [];
56+
}
57+
if (n === 1) {
58+
return [1];
59+
}
60+
const getFirstNFibonacciNumbers: number[] = [1, 1];
61+
62+
for (let i = 2; i < n; i++) {
63+
getFirstNFibonacciNumbers.push(getFirstNFibonacciNumbers[i-1] + getFirstNFibonacciNumbers[1-2]);
64+
}
65+
return getFirstNFibonacciNumbers;
3866
}
3967

4068
/**
@@ -61,6 +89,14 @@ export function binarySearch(
6189

6290
// TODO(you): Finish implementing this algorithm
6391

92+
if (values[pivotIndex] === value) {
93+
return pivotIndex;
94+
} else if (values[pivotIndex] > value) {
95+
return binarySearch(values, start, pivotIndex - 1, value);
96+
} else {
97+
return binarySearch(values, pivotIndex + 1, end, value);
98+
}
99+
64100
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65101
// Else if values[pivotIndex] is greater than the value, then
66102
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;

0 commit comments

Comments
 (0)