Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!