Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda