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
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 Respostas
Responde à pergunta

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 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