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

235
Vistas
why ".length" is acting differently in the similar cases in javaScript?

Here the length is declared before the for loop and the function is giving the correct answer.
The console: []

function dropElements(arr, func) {
  let len = arr.length;

  for (let i = 0; i < len; i++) {
    if (!func(arr[0])) {
      arr.shift();
    }
  }

  return arr;
}

console.log(dropElements([1, 2, 3, 4], function(n) {
  return n > 5;
}));

Now the length is used directy in the for loop and the function is leaving behind two elements of the array. The console: [3,4]

function dropElements(arr, func) {

  for (let i = 0; i < arr.length; i++) {
    if (!func(arr[0])) {
      arr.shift();
    }
  }

  return arr;
}

console.log(dropElements([1, 2, 3, 4], function(n) {
  return n > 5;
}));

about 4 years ago · Santiago Gelvez
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