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