@@ -11,16 +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- if ( distance < 0 ) {
14+ if ( distance < 0 ) {
1515 return - 1 ;
16- }
17- if ( distance > 0 ) {
18- return 1 ;
19- }
20- if ( distance === 0 ) {
16+ } else if ( distance > 0 ) {
2117 return 1 ;
18+ } else {
19+ return 0 ;
2220 }
23- return 0 ;
2421}
2522
2623/**
@@ -40,7 +37,20 @@ export function computeFactorial(n: number): number {
4037 * @return An array containing the first `n` Fibonacci values.
4138 */
4239export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
43- return [ ] ;
40+ /* fibionacci formula - f(n) = f(n-1) + f(n-2)... */
41+ if ( n === 0 ) {
42+ return [ ] ;
43+ }
44+ if ( n === 1 ) {
45+ return [ 1 ] ;
46+ }
47+ // create array
48+ const number : number [ ] = [ 1 , 1 ] ;
49+ for ( let i = 2 ; i < n ; i ++ ) {
50+ //iterate thorugh items in array
51+ number . push ( number [ i - 1 ] + number [ i - 2 ] ) ;
52+ }
53+ return number ;
4454}
4555
4656/**
@@ -71,5 +81,11 @@ export function binarySearch(
7181 // Else if values[pivotIndex] is greater than the value, then
7282 // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
7383 // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
74- return - 1 ;
84+ if ( values [ pivotIndex ] === value ) {
85+ return pivotIndex ;
86+ } else if ( values [ pivotIndex ] > value ) {
87+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
88+ } else {
89+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
90+ }
7591}
0 commit comments