Skip to content
Advertisement

Binary Search in Javascript

I’m trying to implement a binary search algorithm in JavaScript. Things seem okay, but my return statements appear to be returning undefined. Can anybody tell me what’s wrong here?

Fiddle: http://jsfiddle.net/2mBdL/

JavaScript

Advertisement

Answer

You’re not explicitly returning the recursive inner calls (i.e. return binarySearch()), so the call stack unfolds with no return value. Update your code like so:

JavaScript

See a working fiddle

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement