Skip to content

Latest commit

 

History

History
31 lines (29 loc) · 833 Bytes

File metadata and controls

31 lines (29 loc) · 833 Bytes

Screen Shot 2023-03-03 at 1 37 00 PM

/** 
 * Forward declaration of guess API.
 * @param {number} num   your guess
 * @return 	     -1 if num is higher than the picked number
 *			      1 if num is lower than the picked number
 *               otherwise return 0
 * var guess = function(num) {}
 */

/**
 * @param {number} n
 * @return {number}
 */
var guessNumber = function(n) {
    let start = 1, end = n;
    while(true) {
        let mid = Math.floor((start + end) / 2);
        let myGuess = guess(mid);
        if(myGuess === 1) {
            start = mid + 1; 
        } else if(myGuess === -1) {
            end = mid - 1
        } else {
            return mid;
        }
    }
};