Created
November 18, 2014 21:20
-
-
Save shaohua/0752797728ad3ab4a667 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var input = [1,2,3,4,5,6,7,8,9]; | |
| var bs = function(input){ | |
| var middleIndex = Math.ceil(input.length/2) - 1; | |
| var middle = input[middleIndex]; | |
| var target = 6; | |
| //base | |
| if(middle === target){ | |
| return true; | |
| }else if(middle < target){ | |
| return bs( input.slice(middleIndex+1) ); | |
| }else{ | |
| return bs( input.slice(0, middleIndex) ); | |
| } | |
| } | |
| var output = bs(input); | |
| console.log('output', output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment