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

183
Vistas
Code returns [4, 5, 6, 7, 8] instead of [4, 5, 7]. The loop doesn't acknowledge that [6] and [8] are in variable statues

function solution(statues) {
  statues.sort()
  let fullArray = [];
  let missingStatues = [];
  for (let i = Math.min(...statues); i <= Math.max(...statues); i++) {
    fullArray.push(i);
  }
  for (let i = 0; i < fullArray.length; i++) {
    if (!(fullArray[i] in statues)) {
      missingStatues.push(fullArray[i]);
    }
  }
  let missingStatuesCount = missingStatues.length;
  return missingStatues;
}

console.log(solution([6, 2, 3, 8]))

I was expecting [4, 5, 7] but I got [4, 5, 6, 7, 8]. The loop that pushes fullArray[i] into missingStatues, is also pushing [6] and [8], which are in statues and should not be pushed.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

function solution(statues) {
    statues.sort()

The default comparisons made by the .sort() method are string comparisons. Your arrays contain numbers, apparently, so you need a numeric comparator function:

    statues.sort((a, b) => a - b);

Then:

    for (let i=Math.min(...statues);i<=Math.max(...statues);i++) {
        fullArray.push(i);
    }

Once you have successfully sorted the array, statues[0] will be the smallest value and statues[statues.length - 1] will be the largest. There is no need to copy the array and call Math.min() or Math.max().

    for (let i=0;i<fullArray.length;i++) {
        if (!(fullArray[i] in statues)) {
            missingStatues.push(fullArray[i]);
        } 
    }

The in operator is for checking object keys, not values. What you're looking for is the .includes() method:

        if (!statues.includes(fullArray[i]))
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