1

The following is a simple binary search code written in JS. This code is returning -1 whereas it should return 20. Things I have done after looking around a bit:

  1. Replaced "while(min < max)" to "while(min <= max)" would pop up an error on KhanAcademy.

  2. I have used the Math.floor function, which for some reason pops an error "env.Math.floor is not a function" so instead used the "Math.round".

    var doSearch = function(array, targetValue) {
      var min = 0;
      var max = array.length - 1;
      var guess;
      while (min < max) {
        guess = Math.round((max + min) / 2);
        if (array[guess] === targetValue) {
          return guess;
        } else if (array[guess] < targetValue) {
          min = guess + 1;
        } else {
          max = guess - 1;
        }
      }
      return -1;
    };
    var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
    var result = doSearch(primes, 73);
    console.log("Found prime at index " + result);
     //print (primes[]);
     //Program.assertEqual(doSearch(primes, 73), 20);
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
desicoder
  • 11
  • 5

5 Answers5

0

I suggest to change the min rps max value to guess at the assignment of the new values.

Additionally I suggest to change Math.round to Math.floor.

(Small hint: After a return, the if clause has ended, so no need for else if.)

var doSearch = function (array, targetValue) {
    var min = 0;
    var max = array.length - 1;
    var guess;
    while (min < max) {
        guess = Math.floor((max + min) / 2);
        if (array[guess] === targetValue) {
            return guess;
        }
        if (array[guess] < targetValue) {
            min = guess;
        } else {
            max = guess;
        }
    }
    return -1;
};
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
    result = doSearch(primes, 73);

console.log("Found prime at index " + result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    Yes! Thank you for pointing out that small error, the code does produce the intended result but I am still getting an error saying "It looks like you almost have the correct condition on the while loop, but something is still wrong with it." on KhanAcademy – desicoder Oct 12 '16 at 20:22
  • 1. Let min = 0 and max = n-1. 2. If max < min, then stop: target is not present in array. Return -1. 3. Compute guess as the average of max and min, rounded down (so that it is an integer). 4. If array[guess] equals target, then stop. You found it! Return guess. 5. If the guess was too low, that is, array[guess] < target, then set min = guess + 1. 6. Otherwise, the guess was too high. Set max = guess - 1. 7. Go back to step 2 – desicoder Oct 12 '16 at 20:23
0

strong text use (min <= max)

var doSearch = function (array, targetValue) {
    var min = 0;
    var max = array.length - 1;
    var guess;
    while (min <= max) {
        guess = Math.floor((max + min) / 2);
        
        if (array[guess] === targetValue) {
            return guess;
        }
        else if (array[guess] < targetValue) {
            min = guess + 1;
        }
        else {
            max = guess - 1;
        }
    }
    return -1;
};

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 
        41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];

var result = doSearch(primes, 73);
console.log("Found prime at index " + result);
crushwan
  • 1
  • 3
0

This is the full right Solution for the 4 phases.

var doSearch = function(array, targetValue) {
var min = 0;
var max = array.length - 1;
var guess;
var guessTotal = 0;

while(max >= min){
    guess = Math.floor((max + min) / 2);
    println(guess);
    guessTotal++;
    if(array[guess] === targetValue){
        println(guessTotal);
        return guess;
    }
    else if(array[guess] < targetValue){
        min = guess + 1;
    }
    else{
        max = guess - 1;
    }
}
return -1;
};

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 
    41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];

var result = doSearch(primes, 73);
println("Found prime at index " + result);

Program.assertEqual(doSearch(primes, 73), 20);
Program.assertEqual(doSearch(primes, 79), 21);
Program.assertEqual(doSearch(primes, 83), 22);
  • What is the main difference to the code in the question? – greybeard Jul 10 '23 at 09:45
  • 1- the while condition 2- adding math.floor instead of math.round – Abd-ElRhman El Shorbagii Jul 10 '23 at 09:48
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 12 '23 at 01:21
-1

It's in min and max calculation. The "+" and "-" signals are reversed.

    var doSearch = function(array, targetValue) {
      var min = 0;
      var max = array.length - 1;
      var guess;
      while (min < max) {
        guess = Math.round((max + min) / 2);
        if (array[guess] === targetValue) {
          return guess;
        } else if (array[guess] < targetValue) {
          min = guess - 1;
        } else {
          max = guess + 1;
        }
      }
      return -1;
    };
    var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
    var result = doSearch(primes, 73);
    console.log("Found prime at index " + result);
     //print (primes[]);
     //Program.assertEqual(doSearch(primes, 73), 20);
Canilho
  • 944
  • 5
  • 11
  • [Khan Academy Challenge: Binary Search](https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/p/challenge-binary-search) This is my challenge, I still seem to get an error. – desicoder Oct 13 '16 at 17:41
-1

there is just a small error in your code. You should change the min = guess + 1 to min = guess - 1.

    var doSearch = function(array, targetValue) {
      var min = 0;
      var max = array.length - 1;
      var guess;
      while (min < max) {
        guess = Math.round((max + min) / 2);
        if (array[guess] === targetValue) {
          return guess;
        } else if (array[guess] < targetValue) {
          min = guess - 1;
        } else {
          max = guess + 1;
        }
      }
      return -1;
    };
    var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
    var result = doSearch(primes, 73);
    console.log("Found prime at index " + result);
     //print (primes[]);
     //Program.assertEqual(doSearch(primes, 73), 20);
  • [link](https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/p/challenge-binary-search) This is my challenge, I still seem to get an error. – desicoder Oct 13 '16 at 17:40