Skip to content

Commit bec377f

Browse files
committed
fix: lesson7.ts because 1 test was failing
1 parent e383541 commit bec377f

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,24 @@ export function binarySearch(
8585
end: number,
8686
value: number,
8787
): number {
88-
while (start <= end) {
89-
const mid = Math.floor((start + end) / 2);
90-
if (values[mid] === value) {
91-
return mid;
92-
} else if (values[mid] < value) {
93-
start = mid + 1;
94-
} else {
95-
end = mid - 1;
96-
}
88+
if (start > end) {
89+
return -1; // base case: not found
9790
}
98-
99-
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
91+
92+
const pivotIndex = Math.floor((start + end) / 2);
93+
10094
if (values[pivotIndex] === value) {
10195
return pivotIndex;
10296
} else if (values[pivotIndex] > value) {
10397
return binarySearch(values, start, pivotIndex - 1, value);
10498
} else {
10599
return binarySearch(values, pivotIndex + 1, end, value);
106-
return -1;
107100
}
101+
}
108102

109103
// TODO(you): Finish implementing this algorithm
110104

111105
// If values[pivotIndex] is equal to value then return `pivotIndex`.
112106
// Else if values[pivotIndex] is greater than the value, then
113107
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
114-
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
115-
}
108+
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.

0 commit comments

Comments
 (0)