Skip to content

Commit 48c7839

Browse files
committed
wip: binarySearch
1 parent a8399d1 commit 48c7839

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export function computeFactorial(n: number): number {
4343
* @return An array containing the first `n` Fibonacci values.
4444
*/
4545
export function getFirstNFibonacciNumbers(n: number): number[] {
46+
if (n < 0) {
47+
return [];
48+
}
4649
return [];
4750
}
4851

@@ -74,5 +77,13 @@ export function binarySearch(
7477
// Else if values[pivotIndex] is greater than the value, then
7578
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
7679
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
80+
81+
if (value === values[pivotIndex]) {
82+
return pivotIndex;
83+
} else if (value < values[pivotIndex]) {
84+
binarySearch(values, start, pivotIndex - 1, value);
85+
} else {
86+
binarySearch(values, pivotIndex + 1, end, value);
87+
}
7788
return -1;
7889
}

0 commit comments

Comments
 (0)