Last active
December 5, 2018 11:46
-
-
Save rjmacarthy/c770505857149a724e9c0ed907d10b39 to your computer and use it in GitHub Desktop.
Binary Gap JavaScript
This file contains hidden or 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 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