Below is the code I implemented to solve the binary gap problem:
Find longest sequence of zeros, bounded by ones, in binary representation of an integer.
However, it seems to be different from the answers out there. Is it because this is a bad approach in terms of performance? I wanted to get some ideas.
function binaryGap(number) {
let binary = (number >>> 0).toString(2), // convert to binary
regex = /(?!1)(0+)(?=1)/g; // regex to match zeros between 1s
let matches = binary.match(regex);
return matches ? matches.reduce(function(carry, current){
return carry.length > current.length ? carry : current;
}).length : 0;
}