@@ -11,10 +11,13 @@ export function compareStrings(a: string, b: string): number {
1111 // The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
1212 // if it is greater, and 0 if the strings are equal.
1313 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+ }
1821}
1922
2023/**
@@ -24,7 +27,15 @@ export function compareStrings(a: string, b: string): number {
2427 * @return The factorial of n.
2528 */
2629export 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+ }
2839}
2940
3041/**
@@ -34,7 +45,16 @@ export function computeFactorial(n: number): number {
3445 * @return An array containing the first `n` Fibonacci values.
3546 */
3647export 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 ;
3858}
3959
4060/**
@@ -60,7 +80,13 @@ export function binarySearch(
6080 const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
6181
6282 // 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+ }
6490 // If values[pivotIndex] is equal to value then return `pivotIndex`.
6591 // Else if values[pivotIndex] is greater than the value, then
6692 // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
0 commit comments