Skip to content

Commit 4a3d988

Browse files
author
AmiyahJo
committed
fix: binarySearch function
1 parent 8cd4832 commit 4a3d988

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
136136
* @return The index of the value if found in the array and -1 otherwise.
137137
*/
138138
export function binarySearch(
139-
values: number[],
140-
start: number,
141-
end: number,
142-
value: number,
139+
values: number[], start: number, end: number, value: number, value: number,
143140
): number {
144141
if (end < start) {
145142
// The range is not valid so just return -1.
@@ -149,13 +146,13 @@ export function binarySearch(
149146
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
150147

151148
// TODO(you): Finish implementing this algorithm
152-
if (pivotIndex === value) {
149+
if (values[pivotIndex] === value) {
153150
return pivotIndex;
154-
} else if (pivotIndex > value){
155-
return 0;
151+
} else if (values[pivotIndex] > value) {
152+
return binarySearch(values, start, pivotIndex - 1, value);
156153
} else {
157-
return 1;
158-
}
154+
return binarySearch(values, start, pivotIndex + 1, end, value);
155+
}
159156
// If values[pivotIndex] is equal to value then return `pivotIndex`.
160157
// Else if values[pivotIndex] is greater than the value, then
161158
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;

0 commit comments

Comments
 (0)