Skip to content

Commit 12b4c70

Browse files
committed
CHORE: Compare string and compute factorial for lesson7.ts com
plete.
1 parent a4c4623 commit 12b4c70

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ export function compareStrings(a: string, b: string): number {
1212
// if it is greater, and 0 if the strings are equal.
1313
const distance = computeLexicographicDistance(a, b);
1414

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

2024
/**
@@ -24,7 +28,13 @@ export function compareStrings(a: string, b: string): number {
2428
* @return The factorial of n.
2529
*/
2630
export function computeFactorial(n: number): number {
27-
return 0;
31+
let factorial = 1;
32+
33+
for (let i = 1; i < n; n--) {
34+
factorial = factorial * n;
35+
}
36+
37+
return factorial;
2838
}
2939

3040
/**
@@ -61,9 +71,16 @@ export function binarySearch(
6171

6272
// TODO(you): Finish implementing this algorithm
6373

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;
74+
if (values[pivotIndex] === value) {
75+
return pivotIndex;
76+
} else if (values[pivotIndex] > value) {
77+
return binarySearch(values, start, pivotIndex - 1, value);
78+
} else {
79+
return binarySearch(values, pivotIndex + 1, end, value);
80+
}
6981
}
82+
83+
// If values[pivotIndex] is equal to value then return `pivotIndex`.
84+
// Else if values[pivotIndex] is greater than the value, then
85+
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
86+
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.

0 commit comments

Comments
 (0)