Skip to content

Commit 54e40e4

Browse files
committed
fix: lesson_07 array function and binary search
1 parent b437a7d commit 54e40e4

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,18 @@ export function addNumbers(values: number[]): number {
111111
* @return An array containing the first n Fibonacci values.
112112
*/
113113
export function getFirstNFibonacciNumbers(n: number): number[] {
114-
const array = [0, 1]; //populate array with 0,1 for base cases
114+
const array: number[] = [];
115115

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+
}
116126
// starting at 2 increment i until it is = n
117127
for (let i = 2; i < n; i++) {
118128
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(
151161
// call binarySearch(values, start, pivotIndex - 1, value) and return its value;
152162
// Else call binarySearch(values, pivotIndex + 1, end, value) and return its value.
153163
if (values[pivotIndex] === value) return pivotIndex;
154-
else if (values[pivotIndex] > pivotIndex)
164+
else if (values[pivotIndex] > value)
155165
return binarySearch(values, start, pivotIndex - 1, value);
156166
else return binarySearch(values, pivotIndex + 1, end, value);
157167
}

0 commit comments

Comments
 (0)