Skip to content

Commit 0b31f26

Browse files
committed
feat: adding Dasias homework answers
1 parent b3957f4 commit 0b31f26

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,18 @@ export function addNumbers(values: number[]): number {
105105
* @return An array containing the first `n` Fibonacci values.
106106
*/
107107
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 = [];
116112

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;
120118
}
121-
return fibArray;
119+
return numbers;
122120
}
123121

124122
/**
@@ -142,6 +140,13 @@ export function binarySearch(
142140
}
143141

144142
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+
}
145150

146151
// TODO(you): Finish implementing this algorithm
147152

0 commit comments

Comments
 (0)