# Recursive Search: find last values of parameters, after found number

Page 1 of 1

## 5 Replies - 579 Views - Last Post: 04 November 2011 - 07:37 AM

### #1 cdwb77

Reputation: 3
• Posts: 20
• Joined: 01-October 11

# Recursive Search: find last values of parameters, after found number

Posted 03 November 2011 - 06:21 PM

Any advice or clues would be greatly, greatly appreciated. Asking questions here is a last resort. I've spent several hours experimenting on this with various results, none that I wanted though. I will show the snippets of code that I tried using to make it work below the first part that I show you, so you can tell me if I'm on the right path, maybe just didn't have it in the right place. If not, maybe you'll get a good laugh at my feeble attempts.

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,
//    	\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
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?

Is This A Good Question/Topic? 0

## Replies To: Recursive Search: find last values of parameters, after found number

• Changed Man With Different Priorities

Reputation: 949
• Posts: 2,361
• Joined: 15-February 11

## Re: Recursive Search: find last values of parameters, after found number

Posted 04 November 2011 - 06:40 AM

It gives a good explanation as to what undefined is. I can tell you this though.
```"undefined" !== undefined //true
"undefined" !== "not defined" //true
undefined === undefined //true

```
undefined is not a string.

I'm not completely understanding your problem here. What I picked up is you want to perform a binary search and when you've got a match you take the last values from the high and low. Correct me if I'm wrong.

This post has been edited by codeprada: 04 November 2011 - 06:40 AM

### #3 cdwb77

Reputation: 3
• Posts: 20
• Joined: 01-October 11

## Re: Recursive Search: find last values of parameters, after found number

Posted 04 November 2011 - 06:56 AM

Thanks for your response. Yes, that's correct. The user enters a number to be searched, and whether that number is found or not, I have an alert that tells them - found or not found and how many times the search function was called. That part is done, but now I need to also say what the values of hi and lo were, or high and low (if I'm plugging them into variables), ONLY when the number is found.
The most confusing thing to me is that lo and hi are just parameters. I've worked with variables and arrays a little, but parameters are just placeholders, right? That's what makes me think that I need to put them into a variable.

• Changed Man With Different Priorities

Reputation: 949
• Posts: 2,361
• Joined: 15-February 11

## Re: Recursive Search: find last values of parameters, after found number

Posted 04 November 2011 - 07:21 AM

Look on Line #22. B != b.

### #5 cdwb77

Reputation: 3
• Posts: 20
• Joined: 01-October 11

## Re: Recursive Search: find last values of parameters, after found number

Posted 04 November 2011 - 07:35 AM

OMG, that worked! I put line 52 below that alert also and it works. I can't believe that's all I had to do. Thank you so much!

And I don't know how b got pasted as B, but it was already b on my original.

• Changed Man With Different Priorities

Reputation: 949
• Posts: 2,361
• Joined: 15-February 11

## Re: Recursive Search: find last values of parameters, after found number

Posted 04 November 2011 - 07:37 AM