This is how I solved the binary gap problem: Find longest sequence of zeros, bounded by ones, in binary representation of an integer
I wonder how does it fare against solutions such as ones appeared here? Codility binary gap solution using regex
function solution(N) {
const bin = N.toString(2);
let currentGap = 0;
let gaps = [];
for (i=0; i<bin.length; i++){
if (bin[i]==="0"){
currentGap++;
if (bin[i+1]==="1"){
gaps.push(currentGap);
currentGap = 0;
}
}
}
if (gaps.length===1){
return gaps[0];
} else if (gaps.length>1){
return Math.max(...gaps)
} else {
return 0
}
}