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);

5 Answers 5

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);

Sign up to request clarification or add additional context in comments.

2 Comments

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
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
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);

Comments

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);

3 Comments

What is the main difference to the code in the question?
1- the while condition 2- adding math.floor instead of math.round
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.
-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);

1 Comment

Khan Academy Challenge: Binary Search This is my challenge, I still seem to get an error.
-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);

1 Comment

link This is my challenge, I still seem to get an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.