# Codility: BinaryGap Solution

*Lesson 1 Iterations*

1function solution(N) {2 let tempN = N3 let max = 04 let curr = 05 let numOfOne = 06 while (tempN > 0) {7 let rmd = Math.floor(tempN % 2)8 if (rmd === 1) {9 numOfOne += 110 if (numOfOne === 2) {11 if (curr > max) {12 max = curr13 }1415 // reset16 numOfOne = 117 curr = 018 }19 } else if (rmd === 0 && numOfOne === 1) {20 curr += 121 }22 tempN = Math.floor(tempN / 2)23 }24 return max25}

## Comments

Loading comments...

## Tags

codility

## Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

**Fact corner:** Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

**Still hesitate?** Read HoningJS author's guide on dealing with Toptal interview process.