I need to add the

**last values of hi and lo**to either my alert box at the end, when the user's number is found, or in a separate alert box, like I tried to do here unsuccessfully within the search function, in this recursive binary search.

This is the entire code.

var random_number = function ( min, max, digits ) { digits = isNaN(digits) ? 0 : parseInt(digits); if ( digits < 0 ) { digits = 0; } else if ( digits > 16 ) { digits = 16; } if ( digits == 0 ) { return Math.floor( Math.random() * ( max - min + 1 ) ) + min; } else { var rand = Math.random() * ( max - min ) + min; return parseFloat( rand.toFixed(digits) ); } } var numbers = []; for ( var i = 0; i < 100; i++) { numbers[i] = random_number(1,128); } var numeric_order = function (a,B)/> { if ( a < b ) return -1; if ( a > b ) return 1; return 0; } numbers.sort(numeric_order); var fCounter = 0; var search = function(needle, haystack, lo, hi) { fCounter++ ; if ( lo == undefined ) lo = 0; if ( hi == undefined ) hi = haystack.length - 1; var mid = Math.ceil( (hi + lo) / 2 ); if ( hi < lo ) return -1; if ( lo == hi ) { if ( needle == haystack[mid] ) { return mid; alert( "lo = "+ haystack[0] +" hi = "+ haystack[haystack.length - 1] ); //NOTHING HAPPENS } else { return -1; } } if ( haystack[mid] == needle ) { return mid; alert( "lo = "+ haystack[0] +" hi = "+ haystack[haystack.length - 1] ); //NOTHING HAPPENS //alert("lo = "+ lo +" hi = "+ hi ); NOTHING WITH THIS EITHER } else if ( needle < haystack[mid] ) { return search( needle, haystack, lo, mid - 1); } else if ( needle > haystack[mid] ) { return search( needle, haystack, mid + 1, hi); } fCounter += fCounter; } var number, position; do { number = prompt("Number to find (click cancel to quit):"); if ( number == null ) break; number = parseInt(number); if ( isNaN(number) ) continue; position = search(number,numbers); if ( position == -1 ) { alert(number + " is not in the list.\nThe search function was called "+ fCounter +" times."); } else { alert(number + " was found at position " + position + ".\nThe search function was called "+ fCounter +" times."); // IF I COULD STORE THE VALUE OF lo AND hi IN THE VARIABLES low AND high, // I WOULD ADD THE BELOW LINE TO MY ALERT BOX. // \n lo= "+ low +"hi = "+ high ); // IF THERE IS A WAY TO GET THE VALUES WITHOUT STORING // THEM IN A VARIABLE I WOULD USE THIS // \n lo = "+ lo +" hi = "+ hi ); } } while ( true );

Ok, now I know that lo and hi are parameters. There are no arguments or values passed to them, so they equal (==) undefined. When I'm testing this with firebug, it says that lo or low, and hi or high, depending on which one I'm trying at the time is "not defined".

**Are "undefined" and "not defined" the same?**

I tried putting this into the search function after first and second lines that say: return mid; and received the "not defined error message.

lo = lo ; hi = hi ;

same with this:

return lo; return hi;

Then I tried putting the value of lo and hi into variables like this, and got the same errors.

// AT THE BEGINNING OF THE search FUNCTION: var low, high; // THEN I TRIED THIS WITHIN THE search FUNCTION //AND ALSO OUTSIDE- BY ADDING IT TO THE LAST ALERT BOX alert("lo = "+ low +" hi = "+ hi ); // ALSO TRIED SOME THINGS LIKE THIS low = haystack[0] low = haystack[lo] //grasping at straws here high = haystack[haystack.length -1]

I am really not understanding how these undefined parameters work. I can't find a good explanation in any text book I have (I have 3) or online either.

Am I anywhere close to being on the right track here?