Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda