Created
May 10, 2019 12:50
-
-
Save ecaepsey/260261fb92cd74d606ff8c401141eeb4 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/xagihiy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var binarySearch = function(array, value) { | |
var guess, | |
min = 0, | |
max = array.length - 1; | |
while(min <= max){ | |
guess = Math.floor((min + max) /2); | |
if(array[guess] === value) | |
return guess; | |
else if(array[guess] < value) | |
min = guess + 1; | |
else | |
max = guess - 1; | |
} | |
return -1; | |
} | |
console.log(binarySearch([1,2,3,4, 0,1,8, 9], 9)) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var binarySearch = function(array, value) { | |
var guess, | |
min = 0, | |
max = array.length - 1; | |
while(min <= max){ | |
guess = Math.floor((min + max) /2); | |
if(array[guess] === value) | |
return guess; | |
else if(array[guess] < value) | |
min = guess + 1; | |
else | |
max = guess - 1; | |
} | |
return -1; | |
} | |
console.log(binarySearch([1,2,3,4, 0,1,8, 9], 9))</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var binarySearch = function(array, value) { | |
var guess, | |
min = 0, | |
max = array.length - 1; | |
while(min <= max){ | |
guess = Math.floor((min + max) /2); | |
if(array[guess] === value) | |
return guess; | |
else if(array[guess] < value) | |
min = guess + 1; | |
else | |
max = guess - 1; | |
} | |
return -1; | |
} | |
console.log(binarySearch([1,2,3,4, 0,1,8, 9], 9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment