@@ -11,6 +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+ if ( distance < 0 ) {
16+ return - 1 ;
17+ } else if ( distance > 0 ) {
18+ return 1 ;
19+ }
20+ }
1421
1522 // TODO(you): Finish this method.
1623
@@ -24,7 +31,9 @@ export function compareStrings(a: string, b: string): number {
2431 * @return The factorial of n.
2532 */
2633export 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 ) ;
2837}
2938
3039/**
@@ -34,7 +43,15 @@ export function computeFactorial(n: number): number {
3443 * @return An array containing the first `n` Fibonacci values.
3544 */
3645export 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 ;
3855}
3956
4057/**
@@ -60,6 +77,13 @@ export function binarySearch(
6077 const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
6178
6279 // 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 ) ;
6387
6488 // If values[pivotIndex] is equal to value then return `pivotIndex`.
6589 // Else if values[pivotIndex] is greater than the value, then
0 commit comments