# Why does my binary search code works only for some targets and not for others

here is my code:

``````let array = [1,2,3,4,5,6,7,8,9,10];
function searcher(arr, target){
let start = 0;
let end = arr.length-1;
let mid = 0;
let a = 100;
while(a>0){
a--;
mid = Math.floor(start+end/2);
if(arr[mid] == target){
return mid;
}else{
if(arr[mid] < target){
start = mid+1;
}else{
end = mid-1
}
}
}
return "doesn't exist";

}
console.log(searcher(array, 6));
``````

this code can get indexes of the numbers 1 and 2 and 4 and 5 but not for 3 and 6 and 7 and 8 and 9 and 10, it throws: â€śdoesnâ€™t existâ€ť instead, which means the loop ends before it finds the value even though all those values exist in the array given to the function.
I tried this from a freecodecamp video Treehouse created, it talks about algorithms.

can any body tell me why my code doesnâ€™t work all the time or how to improve it?
thank you sir/mem/miss/he/she/they

My first step would be to put in some logging to see what is happeningâ€¦

``````function searcher(arr, target){
let start = 0;
let end = arr.length-1;
let mid = 0;
let a = 100;
while(a>0){
a--;
mid = Math.floor(start+end/2);
console.log(`start=\${start} end=\${end} mid=\${mid}`)
if(arr[mid] == target){
return mid;
}else{
if(arr[mid] < target){
start = mid+1;
}else{
end = mid-1
}
}
}
return "doesn't exist";
}
``````

after the loop goes once, it shows that mid is equal to 9 and so for the rest of the looping, the start changes once to a closer value to the target and then it doesnâ€™t change again.
here:

``````PS C:\Users\HP\desktop> node binarySearch.js
start=0 end=9 mid=4
start=5 end=9 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
start=5 end=8 mid=9
doesn't exist
PS C:\Users\HP\desktop>
``````

this is for target = 6;

seems like mid value doesnâ€™t change in accordance.
mid value gets stuck back to the value of 9 for the rest of the looping.

I solved it by trying if each of the start and end values can be the index of the target throughout the loop, it gave the right answer for all of the value of target.
here:

``````let array = [1,2,3,4,5,6,7,8,9,10];
function searcher(arr, target){
let start = 0;
let end = arr.length-1;
let mid = 0;
let a = 100;
while(a>0){
a--;
mid = Math.floor((start + end) / 2);
console.log(`start=\${start} end=\${end} mid=\${mid}`)
if(arr[mid] == target){
return mid;
}else{
if(arr[mid] < target){
start = mid+1;
if(arr[start] == target){
return start;
}
if(arr[end] == target){
return end
}

}else{
end = mid-1
if(arr[start] == target){
return start;
}
if(arr[end] == target){
return end
}
}
}
}
return "doesn't exist";
}
console.log(searcher(array, 10));
``````

THANK YOU KEVIN

Itâ€™s important to note i think, that in the treehouse video, they solved the problem without trying the end and start values, and it worked for them using python.

Right, and do you understand why this didnâ€™t work?

``````mid = Math.floor(start+end/2);
``````

You fixed it but didnâ€™t acknowledge it.

Itâ€™s important to note i think, that in the treehouse video, they solved the problem without trying the end and start values, and it worked for them using python.

Ideally the language shouldnâ€™t matter. Algorithms are not language specific. The devil in the details is expressing that algorithm.

For what itâ€™s worth, this is what I came up with.

``````const searcher = (arr, target) => {
let start = 0
let end = arr.length - 1

while (start <= end) {
const mid = Math.floor((start + end) / 2)

if (arr[mid] === target)
return mid

if (arr[mid] < target)
start = mid + 1
else
end = mid - 1
}

return 'doesn\'t exist'
}

console.log(searcher([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 6))
// 5

console.log(searcher([1, 3, 5, 100], 1))
// 0

console.log(searcher([1, 3, 5, 100], 3))
// 1

console.log(searcher([1, 3, 5, 100], 5))
// 2

console.log(searcher([1, 3, 5, 100], 100))
// 2

console.log(searcher([1, 3, 5, 100], 101))
// doesn't exist

console.log(searcher([1, 3, 5, 100], 99))
// doesn't exist

console.log(searcher([1, 3, 5, 100], 2))
// doesn't exist

console.log(searcher([1, 3, 5, 100], 0))
// doesn't exist

console.log(searcher([1, 3, 5, 7, 100], 1))
// 0

console.log(searcher([1, 3, 5, 7, 100], 3))
// 1

console.log(searcher([1, 3, 5, 7, 100], 5))
// 2

console.log(searcher([1, 3, 5, 7, 100], 7))
// 3

console.log(searcher([1, 3, 5, 7, 100], 100))
// 4
``````

You mean it didnâ€™t work because i didnâ€™t put the sum of start and end:

``````(start+end/2)
``````

In parenthesis like this:

``````((start+end)/2)
``````

Right?

Right, because if order of operations, `start+end/2` is the same as `start+(end/2)`.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.