Skip to content

Instantly share code, notes, and snippets.

@rjmacarthy
Last active December 5, 2018 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjmacarthy/c770505857149a724e9c0ed907d10b39 to your computer and use it in GitHub Desktop.
Save rjmacarthy/c770505857149a724e9c0ed907d10b39 to your computer and use it in GitHub Desktop.
Binary Gap JavaScript
var tests = [234534534234];
tests.forEach(function (t) {
Test(t);
});
function Test(N) {
var bin = N.toString(2);
var b = 0;
var col = bin.split("1").filter(function (val) {
return val ? true : false;
});
if (col.length) {
if (bin.slice(-1) != '1') {
col.splice(col.length - 1, 1)
}
col.forEach(function (val) {
val.length > b ? b = val.length : b = b;
});
}
return b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment