File tree Expand file tree Collapse file tree 1 file changed +17
-12
lines changed
lesson_07/conditionals/src Expand file tree Collapse file tree 1 file changed +17
-12
lines changed Original file line number Diff line number Diff line change @@ -105,20 +105,18 @@ export function addNumbers(values: number[]): number {
105
105
* @return An array containing the first `n` Fibonacci values.
106
106
*/
107
107
export function getFirstNFibonacciNumbers ( n : number ) : number [ ] {
108
- if ( n === 0 ) {
109
- return [ ] ;
110
- } else if ( n === 1 ) {
111
- return [ 1 ] ;
112
- } else if ( n === 2 ) {
113
- return [ 1 , 1 ] ;
114
- }
115
- const fibArray = [ 1 , 1 ] ;
108
+ let current = 1 ;
109
+ let prev = 0 ;
110
+
111
+ const numbers = [ ] ;
116
112
117
- for ( let i = 2 ; i < n ; i ++ ) {
118
- const nextFibNumber = fibArray [ i - 1 ] + fibArray [ i - 2 ] ;
119
- fibArray . push ( nextFibNumber ) ;
113
+ for ( let i = 1 ; i <= n ; i ++ ) {
114
+ numbers . push ( current ) ;
115
+ const nextNumber = current + prev ;
116
+ prev = current ;
117
+ current = nextNumber ;
120
118
}
121
- return fibArray ;
119
+ return numbers ;
122
120
}
123
121
124
122
/**
@@ -142,6 +140,13 @@ export function binarySearch(
142
140
}
143
141
144
142
const pivotIndex = Math . floor ( ( start + end ) / 2 ) ; // The index in the middle of the array.
143
+ if ( values [ pivotIndex ] === value ) {
144
+ return pivotIndex ;
145
+ } else if ( values [ pivotIndex ] > value ) {
146
+ return binarySearch ( values , start , pivotIndex - 1 , value ) ;
147
+ } else {
148
+ return binarySearch ( values , pivotIndex + 1 , end , value ) ;
149
+ }
145
150
146
151
// TODO(you): Finish implementing this algorithm
147
152
You can’t perform that action at this time.
0 commit comments