I've recently been practicing writing recursive functions in JavaScript, and I've been running into a problem consistently when it comes to return values, which is that they ALWAYS seem to be undefined when returned from inside a conditional block.
Here is an example on jsFiddle
https://jsfiddle.net/g0dm68nm/
This is a simple binary sort implemented recursively. At this block of code
if (array[guess] === targetValue){
console.log("equal");
return(guess);
}
I see in my console "equal", which would never happen unless
A) The variable guess is defined
B) The item located at the index of that array equals the target search value
So I know that guess is absolutely defined or else my function would never evaluate the console.log statement, but the value returned is always undefined.
What am I missing? What is it that I am not understanding about recursion? This is the third function in the last 2 weeks I've written that behaves in the same way and I can't find any sort of answer anywhere online.