@@ -12,9 +12,13 @@ export function compareStrings(a: string, b: string): number {
12
12
// if it is greater, and 0 if the strings are equal.
13
13
const distance = computeLexicographicDistance ( a , b ) ;
14
14
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
+ }
18
22
}
19
23
20
24
/**
@@ -24,7 +28,13 @@ export function compareStrings(a: string, b: string): number {
24
28
* @return The factorial of n.
25
29
*/
26
30
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 ;
28
38
}
29
39
30
40
/**
@@ -61,9 +71,16 @@ export function binarySearch(
61
71
62
72
// TODO(you): Finish implementing this algorithm
63
73
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
+ }
69
81
}
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