@@ -11,10 +11,14 @@ 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-
14+ if ( distance < 0 ) {
15+ return - 1 ;
16+ } else if ( distance > 0 ) {
17+ return 1 ;
18+ } else {
19+ return 0 ;
20+ }
1521 // TODO(you): Finish this method.
16-
17- return 0 ;
1822}
1923
2024/**
@@ -24,7 +28,16 @@ export function compareStrings(a: string, b: string): number {
2428 * @return The factorial of n.
2529 */
2630export function computeFactorial ( n : number ) : number {
27- return 0 ;
31+ if ( n < 0 ) {
32+ return 0 ;
33+ }
34+ let numbers = 1 ;
35+
36+ for ( let i = 1 ; i < n ; n -- ) {
37+ numbers = numbers * n ;
38+ }
39+
40+ return numbers ;
2841}
2942
3043/**
@@ -34,6 +47,16 @@ export function computeFactorial(n: number): number {
3447 * @return An array containing the first `n` Fibonacci values.
3548 */
3649export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
50+ if ( n === 0 ) {
51+ return [ ] ;
52+ }
53+ if ( n === 1 ) {
54+ return [ 1 ] ;
55+ }
56+ const number : number [ ] = [ 1 , 1 ] ;
57+ for ( let i = 2 ; i < n ; i ++ ) {
58+ number . push ( number [ i - 1 ] + number [ i - 2 ] ) ;
59+ }
3760 return [ ] ;
3861}
3962
@@ -65,5 +88,12 @@ export function binarySearch(
6588 // Else if values[pivotIndex] is greater than the value, then
6689 // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
6790 // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
91+ if ( values [ pivotIndex ] === value ) {
92+ return pivotIndex ;
93+ } else if ( values [ pivotIndex ] > value ) {
94+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
95+ } else {
96+ return binarySearch ( values , start , pivotIndex + 1 , end ) ;
97+ }
6898 return - 1 ;
6999}
0 commit comments