Learning on Codility

Codility is where programmers receive programming tests as interview questions. This is also where programmers could learn to code. I am at lesson 1 on Iterations, and here’s my answer. It finds the binary gap of a number. A binary gap is the longest sequence of 0 of a number converted to binary. For example, the number 5 is 101 is binary, and it contains 1 zero, so it’s binary gap is 1. The number 1041 is 10000010001 in binary. It contains 2 sequences of 0, the first of which is 5 zeros long and the second is 0 zeros long. So its binary gap is 5, since the first sequence is longer than the first.

I got 100% and did it in 15 minutes and….2 tries. During my first try, I got excited and forgot to initialize my numZero variable back to 0 after it found each sequence of zeros. Careless me!

function mysolution(N) {
	var numZero = 0;
	var found = 0;
	var con = (N >>> 0).toString(2);
	for(var i= 0; i<= con.length-1; i++){
		if (con.substring(i,i+1) == "0"){
		else {
			if (numZero > found){
				found = numZero;
			numZero = 0;
	return found;

Try it too! https://codility.com/programmers/

Photo by Ilya Pavlov on Unsplash