File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed
lesson_07/conditionals/src Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -111,8 +111,18 @@ export function addNumbers(values: number[]): number {
111
111
* @return An array containing the first n Fibonacci values.
112
112
*/
113
113
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
114
- const array = [ 0 , 1 ] ; //populate array with 0,1 for base cases
114
+ const array : number [ ] = [ ] ;
115
115
116
+ if ( n === 0 ) {
117
+ return array ;
118
+ }
119
+
120
+ if ( n >= 1 ) {
121
+ array . push ( 1 ) ; // Add the first Fibonacci number
122
+ }
123
+ if ( n >= 2 ) {
124
+ array . push ( 1 ) ;
125
+ }
116
126
// starting at 2 increment i until it is = n
117
127
for ( let i = 2 ; i < n ; i ++ ) {
118
128
const Fibonacci = array [ i - 1 ] + array [ i - 2 ] ; //add the numbers 2 positions behind to get current position
@@ -151,7 +161,7 @@ export function binarySearch(
151
161
// call binarySearch(values, start, pivotIndex - 1, value) and return its value;
152
162
// Else call binarySearch(values, pivotIndex + 1, end, value) and return its value.
153
163
if ( values [ pivotIndex ] === value ) return pivotIndex ;
154
- else if ( values [ pivotIndex ] > pivotIndex )
164
+ else if ( values [ pivotIndex ] > value )
155
165
return binarySearch ( values , start , pivotIndex - 1 , value ) ;
156
166
else return binarySearch ( values , pivotIndex + 1 , end , value ) ;
157
167
}
You can’t perform that action at this time.
0 commit comments