Skip to content

Instantly share code, notes, and snippets.

@peterschmiz
Created April 23, 2016 06:52
Show Gist options
  • Save peterschmiz/4364f6f037feb2c51a9169a338492de7 to your computer and use it in GitHub Desktop.
Save peterschmiz/4364f6f037feb2c51a9169a338492de7 to your computer and use it in GitHub Desktop.
function solution(N) {
return parseInt(N, 10).
toString(2).
replace(/^0+|0+$/g, '').
split('1').
reduce(function (a, b) {
return a.length > b.length ? a : b;
}).length
}
@michaelNos
Copy link

Why you replacing the zero's (replace(/^0+|0+$/g, ''))? It can also works without.
Like that: return Number(N).toString(2).split('1').reduce(function (a, b) { return a.length > b.length ? a : b;}).length;
I just wont to understand the replace use. TY!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment