DININDU - 7 months ago 20

Javascript Question

I have created function named

`getIndex`

When i changed

`while (1)`

`while (max < min)`

`while (max < min)`

`data`

`// assume my array looks-like this.`

var data = [1, 2, 3, 4, 5, 7, 8, 9, 10];

function getIndex(search, arr) {

var min, max, guess, count;

min = 0;

max = arr.length - 1;

count = 0;

// If my search is not present in array. Return -1.

while (max < min) {

count++;

guess = Math.floor((min + max) / 2);

if (arr[guess] === search) {

return guess;

} else if (arr[guess] < search) {

min = guess + 1;

} else {

max = guess - 1;

}

}

return -1;

}

getIndex(8, data); // return -1