@@ -11,16 +11,13 @@ export function compareStrings(a: string, b: string): number {
11
11
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
12
12
// if it is greater, and 0 if the strings are equal.
13
13
const distance = computeLexicographicDistance ( a , b ) ;
14
- if ( distance < 0 ) {
14
+ if ( distance < 0 ) {
15
15
return - 1 ;
16
- }
17
- if ( distance > 0 ) {
18
- return 1 ;
19
- }
20
- if ( distance === 0 ) {
16
+ } else if ( distance > 0 ) {
21
17
return 1 ;
18
+ } else {
19
+ return 0 ;
22
20
}
23
- return 0 ;
24
21
}
25
22
26
23
/**
@@ -40,7 +37,20 @@ export function computeFactorial(n: number): number {
40
37
* @return An array containing the first `n` Fibonacci values.
41
38
*/
42
39
export 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 ;
44
54
}
45
55
46
56
/**
@@ -71,5 +81,11 @@ export function binarySearch(
71
81
// Else if values[pivotIndex] is greater than the value, then
72
82
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
73
83
// 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
+ }
75
91
}
0 commit comments