Skip to content

Instantly share code, notes, and snippets.

@mayashavin
Created February 10, 2018 18:03
Show Gist options
  • Save mayashavin/f23981ec4a29b02a4461856e705addbb to your computer and use it in GitHub Desktop.
Save mayashavin/f23981ec4a29b02a4461856e705addbb to your computer and use it in GitHub Desktop.
function firstBadVersion(arr){
let found = undefined, start = 0, end = arr.length;
while (start < end && !found){
let mid = Math.floor((start + end)/2);
if (isBad(arr[mid])){
if (arr[mid - 1] && isBad(arr[mid - 1])){
end = mid - 1;
}
else{
found = mid;
}
}
else{
start = mid + 1;
}
}
return found;
}
function isBad(version){
return version === 'B';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment