@@ -11,10 +11,13 @@ 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
-
15
- // TODO(you): Finish this method.
16
-
17
- return 0 ;
14
+ if ( distance < 0 ) {
15
+ return - 1 ;
16
+ } else if ( distance > 0 ) {
17
+ return 1 ;
18
+ } else {
19
+ return 0 ;
20
+ }
18
21
}
19
22
20
23
/**
@@ -24,7 +27,15 @@ export function compareStrings(a: string, b: string): number {
24
27
* @return The factorial of n.
25
28
*/
26
29
export function computeFactorial ( n : number ) : number {
27
- return 0 ;
30
+ if ( n < 0 ) {
31
+ return 0 ;
32
+ } else {
33
+ let result = 1 ;
34
+ for ( let i = 2 ; i <= n ; i ++ ) {
35
+ result = result * i ;
36
+ }
37
+ return result ;
38
+ }
28
39
}
29
40
30
41
/**
@@ -34,7 +45,16 @@ export function computeFactorial(n: number): number {
34
45
* @return An array containing the first `n` Fibonacci values.
35
46
*/
36
47
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
37
- return [ ] ;
48
+ if ( n === 0 ) {
49
+ return [ ] ;
50
+ }
51
+ const resultsArr : number [ ] = [ 1 , 1 ] ;
52
+ if ( n > 1 ) {
53
+ for ( let i = 2 ; i < n ; i ++ ) {
54
+ resultsArr . push ( resultsArr [ i - 1 ] + resultsArr [ i - 2 ] ) ;
55
+ }
56
+ }
57
+ return resultsArr ;
38
58
}
39
59
40
60
/**
@@ -60,7 +80,13 @@ export function binarySearch(
60
80
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
61
81
62
82
// TODO(you): Finish implementing this algorithm
63
-
83
+ if ( values [ pivotIndex ] === value ) {
84
+ return pivotIndex ;
85
+ } else if ( values [ pivotIndex ] < value ) {
86
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
87
+ } else {
88
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
89
+ }
64
90
// If values[pivotIndex] is equal to value then return `pivotIndex`.
65
91
// Else if values[pivotIndex] is greater than the value, then
66
92
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
0 commit comments