@@ -11,10 +11,14 @@ export function compareStrings(a: string, b: string): number {
11
11
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
12
12
// if it is greater, and 0 if the strings are equal.
13
13
const distance = computeLexicographicDistance ( a , b ) ;
14
-
14
+ if ( distance < 0 ) {
15
+ return - 1 ;
16
+ } else if ( distance > 0 ) {
17
+ return 1 ;
18
+ } else {
19
+ return 0 ;
20
+ }
15
21
// TODO(you): Finish this method.
16
-
17
- return 0 ;
18
22
}
19
23
20
24
/**
@@ -24,7 +28,16 @@ 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
+ if ( n < 0 ) {
32
+ return 0 ;
33
+ }
34
+ let numbers = 1 ;
35
+
36
+ for ( let i = 1 ; i < n ; n -- ) {
37
+ numbers = numbers * n ;
38
+ }
39
+
40
+ return numbers ;
28
41
}
29
42
30
43
/**
@@ -34,6 +47,16 @@ export function computeFactorial(n: number): number {
34
47
* @return An array containing the first `n` Fibonacci values.
35
48
*/
36
49
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
50
+ if ( n === 0 ) {
51
+ return [ ] ;
52
+ }
53
+ if ( n === 1 ) {
54
+ return [ 1 ] ;
55
+ }
56
+ const number : number [ ] = [ 1 , 1 ] ;
57
+ for ( let i = 2 ; i < n ; i ++ ) {
58
+ number . push ( number [ i - 1 ] + number [ i - 2 ] ) ;
59
+ }
37
60
return [ ] ;
38
61
}
39
62
@@ -65,5 +88,12 @@ export function binarySearch(
65
88
// Else if values[pivotIndex] is greater than the value, then
66
89
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
67
90
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
91
+ if ( values [ pivotIndex ] === value ) {
92
+ return pivotIndex ;
93
+ } else if ( values [ pivotIndex ] > value ) {
94
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
95
+ } else {
96
+ return binarySearch ( values , start , pivotIndex + 1 , end ) ;
97
+ }
68
98
return - 1 ;
69
99
}
0 commit comments