Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var indexOf = function(arr, target) {
  var result = -1;
  var min = 0;
  var max = arr.length - 1;
  
  while (min <= max) {
    // Divide range in half and check value.
    var index = Math.floor(((max - min) / 2) + min);
    console.log('min=' + min + ', max=' + max + ', index=' + index);
    
    if (arr[index] === target) {
      // Found our target, we're done!
      result = index;
      break;
    }
    else if (arr[index] < target) {
      // Target is higher.
      min = index + 1;
    }
    else {
      // Target is lower.
      max = index - 1;
    }
  }
  
  return result;
}
var numbers = [];
for (var i=500; i<100000; i++) {
  numbers.push(i);
}
console.log(indexOf(numbers, 12345));
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers