Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Find longest sequence of zeros in binary representation of an integer in JavaScript?
  1. Well in this question I have to find the longest sequence of zeros in the binary representation of an integer.
  2. After a lot of hard work, I'm able to find the longest sequence of zeros with my logic which I changed many times.
  3. I have one problem with my logic which is if I input an integer that has no gap in it has to give me an answer 0 which I tried to make by myself but I failed.
  4. Right now if I input an integer that has no gap it gives me output infinity.
  5. I want my answer to print 0 when the binary representation of an integer has no gap in it.

var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];

while (dn != 0) {
  bn[i] = dn % 2;
  dn = Math.floor(dn / 2);
  i++;
}

for (var j = i - 1; j >= 0; j--) {
  binary.push(bn[j]);
}
console.log(binary.join(""));

var currentGap = 0;
var gaps = [];
var len = binary.length;

for (var k = 0; k < len; k++) {
  if (binary[k] == 0) {
    currentGap++;
    if (binary[k + 1] == 1) {
      gaps.push(currentGap);
      currentGap = 0;
    }
  }
}

console.log(Math.max(...gaps));

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could add initialize gaps with 0, or add condition when gaps remains empty to print 0 else max of gaps.

var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];

while (dn != 0) {
  bn[i] = dn % 2;
  dn = Math.floor(dn / 2);
  i++;
}

for (var j = i - 1; j >= 0; j--) {
  binary.push(bn[j]);
}
console.log(binary.join(""));

var currentGap = 0;
//var gaps = []
var gaps = [0];
var len = binary.length;

for (var k = 0; k < len; k++) {
  if (binary[k] == 0) {
    currentGap++;
    if (binary[k + 1] == 1) {
      gaps.push(currentGap);
      currentGap = 0;
    }
  }
}
//if (gaps.length === 0)
//  console.log(0)
//else
//  console.log(Math.max(...gaps));
console.log(Math.max(...gaps));

about 4 years ago · Juan Pablo Isaza Report

0

You can simply print 0 if your gaps array has no length.

var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];

while (dn != 0) {
  bn[i] = dn % 2;
  dn = Math.floor(dn / 2);
  i++;
}

for (var j = i - 1; j >= 0; j--) {
  binary.push(bn[j]);
}
console.log(binary.join(""));

var currentGap = 0;
var gaps = [];
var len = binary.length;

for (var k = 0; k < len; k++) {
  if (binary[k] == 0) {
    currentGap++;
    if (binary[k + 1] == 1) {
      gaps.push(currentGap);
      currentGap = 0;
    }
  }
}

console.log(gaps.length ? Math.max(...gaps) : 0); // <-- This

about 4 years ago · Juan Pablo Isaza Report

0

I apologize if I'm misunderstanding your question, but would this satisfy your problem?

const s = prompt("Enter a number:");
const longest = Math.max(...parseInt(s).toString(2).split('1').map(s=>s.length));

console.log(longest);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!