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