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

191
Vistas
Why is my code returning " [ undefined, undefined, undefined ]"?

function sumAll(arr) {
  arr.sort();
  let lum = [];
  for (let i = arr[1]; i > arr[0]; i--) {
    lum.push(arr[i]);
  }
  return lum;
}

console.log(sumAll([1, 4]));

I want my code to do 4-3, then add the result to lum and so on until it reaches 1 but i'm stuck at this point. I don't know why it returns undefined.

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

0

You're pushing arr[i] onto the result:

lum.push(arr[i]);

So for example when i is 4 (because arr[1] is 4), what is the value arr[4] that you're pushing to the result? It's undefined in the array provided. I suspect you meant to just push i to the result?:

lum.push(i);

For example:

function sumAll(arr) {
  arr.sort();
  let lum = [];
  for (let i = arr[1]; i > arr[0]; i--) {
    lum.push(i);
  }
  return lum;
}

console.log(sumAll([1, 4]));

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