Guest User

Untitled

a guest
Nov 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. const myArray = [1, 3, 7, 9, 10, 20];
  2.  
  3. function binarySearch(arr, value) {
  4. let first = 0;
  5. let last = arr.length - 1;
  6. let middle = Math.floor((first + last) / 2);
  7.  
  8. if (arr[middle] > value) {
  9. const newArr = arr.slice(0, middle);
  10. return binarySearch(newArr, value);
  11. } else if (arr[middle] < value) {
  12. const newArr = arr.slice(middle + 1, arr.length);
  13. return binarySearch(newArr, value);
  14. } else {
  15. return true;
  16. }
  17. }
  18.  
  19. const anum = binarySearch(myArray, 10);
  20.  
  21. console.log(anum)
Add Comment
Please, Sign In to add comment