1

I am trying to finish a quiz on Khan Academy. It asks me to only print the total number of guesses when it has found the target.

The link to the quiz: link

/* Returns either the index of the location in the array,
  or -1 if the array did not contain the targetValue */
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;
            println(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];

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

Program.assertEqual(doSearch(primes, 73), 20);
user812786
  • 4,302
  • 5
  • 38
  • 50
th3phacee
  • 9
  • 3
  • What part of your code runs when it has found the target? Can you run your `println(guess)` when that happens? Also, you have a syntax error. You're missing a `}` at the end of your first `if` block. – Mike Cluck Aug 18 '16 at 19:32
  • i tested your code, and if you add the missing `}`, your code passes the test on the last line. – davidhu Aug 18 '16 at 19:44

3 Answers3

1

This is the code I used that worked:

    /* Returns either the index of the location in the array,
  or -1 if the array did not contain the targetValue */
var doSearch = function(array, targetValue) {
    var min = 0;
    var max = array.length - 1;
    var guess;
    var guesscount = 0;
    while( min <= max ){
        guess = Math.floor((min + max) / 2);
        println(guess);
        guesscount = guesscount + 1;
        if( array[guess] === targetValue ){
            println("Found prime in " + guesscount + " guesses");
            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);
JamesGhost
  • 11
  • 1
0

Try adding a global variable holding the number of guesses and increment this whenever a new guess i processed. Set it to zero at the start of each new search.

When you've gotten that far, you can put the global variable into the doSearch function and change the doSearch functions return type to an array. The array can then hold both the original return value and the number of guesses.

0

You need a variable to keep count on your guesses. Add

var guesscount;

to your function body where your other variables are defined, then in the while-loop increment it by adding

guesscount = guesscount + 1;

before your if-statements. You can then print the result using

println("Number of guesses: " + guesscount);

S. Wenzel
  • 81
  • 5