I am looking at how to implement binary search in a javascript function and found that when I return the value and save it into a variable then when I console.log this comes as undefined.
const recursiveBinarySearch = (numbers, target) => {
const midpoint = Math.floor(numbers.length / 2);
if (numbers[midpoint] === target){
//it does found the value and return
return 'FOUND';
} else if(numbers[midpoint] < target) {
recursiveBinarySearch(numbers.slice(midpoint+1), target);
} else {
recursiveBinarySearch(numbers.slice(midpoint-1), target);
}
}
var result = recursiveBinarySearch([1, 2, 3, 4, 6, 8, 100] , 8);
console.log(result); // Here is returning undefined
Thanks in advance.
returnthe result of the recursion.