@@ -11,6 +11,15 @@ 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+ if ( distance < 0 ) {
16+ return - 1 ;
17+ } else if ( distance > 0 ) {
18+ return 1 ;
19+ } else {
20+ return 0 ;
21+ }
22+ }
1423
1524 // TODO(you): Finish this method.
1625
@@ -24,7 +33,15 @@ export function compareStrings(a: string, b: string): number {
2433 * @return The factorial of n.
2534 */
2635export 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+ }
2845}
2946
3047/**
@@ -34,7 +51,15 @@ export function computeFactorial(n: number): number {
3451 * @return An array containing the first `n` Fibonacci values.
3552 */
3653export 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 ;
3863}
3964
4065/**
@@ -60,7 +85,13 @@ export function binarySearch(
6085 const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
6186
6287 // 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+ }
6495 // If values[pivotIndex] is equal to value then return `pivotIndex`.
6596 // Else if values[pivotIndex] is greater than the value, then
6697 // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
0 commit comments