Codility binary gap solution
Here's my approach:

function solution(n) { 
    var maxZeros = 0; 
    while(n !== 0 &amp;&amp; n % 2 === 0) {
        n &gt;&gt;&gt;= 1;
    }
    for(var curr=0; n !== 0; n&gt;&gt;&gt;=1) { 
        if(n % 2 === 0) { 
            curr++; 
        } else { 
            curr = 0; 
        } 
        maxZeros = Math.max(maxZeros, curr); 
    } 
    return maxZeros; 
}


Some notable differences from your solution:


Number isn't converted to binary string.  This is half for optimization purposes and half for lack of necessity.
Approach to handling the "zero gap must be bound by 1s" requisite.  The first 1 is automatically handled because it wouldn't be zero if there were still a 1 to handle.  However the lower 1 bound is simply handled by shifting until the first 1 is in the lowest digit, eliminating the need to add flags or extra handling.
Notice that no memory is required to hold gap information.  It is irrelevant as you can save only the information required as you move along.  
Value n is checked against being 0 as opposed to being greater than zero just because a negative number should not be disregarded just because it is negative.


Hope that helps!  If you have any questions, just ask!