DEV Community

Discussion on: Binary Search in JS

Collapse
 
curtisfenner profile image
Curtis Fenner

There's a few things that can make this code much nicer.

First, there's no reason to make middle be a parameter. Notice how you're computing it the same way in two different places?

Second, an "early return" will eliminate the nesting, making the logic much simpler -- although you could also replace the recursion with a while loop.

Collapse
 
atila profile image
Atila Fassina

Thank you, Curtis.
I updated the code with some of your suggestions 🙂