Ad

How To Find/print Return Value Of A Function?

- 1 answer

Doing a basic binary search problem, but how do you know if it actually works if you cant see the number that's being returned??

function binarySearch(arr, val) {
    let start = 0;
    let end = arr.length - 1;
  
    while (start <= end) {
      let mid = Math.floor((start + end) / 2);
  
      if (arr[mid] === val) {
        return mid;
      }
  
      if (val < arr[mid]) {
        end = mid - 1;
      } else {
        start = mid + 1;
      }
    }
    return -1;
}

let arr = [1,9,3,4,5,7,2];

binarySearch(arr, 9);
Ad

Answer

If you want to debug or just see the print , you can try debugging option in any of the code editor or just add print before return statements with time , to realize what is returned

Ad
source: stackoverflow.com
Ad