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

92
Views
Running sum until a negative number

Please how can I use the index 0 in a for-loop when testing an empty array for a drumming sum. Also, the single negative value returns zero The sum should stop when a negative number is encountered

  let sum = 0;

  let lenArr = arr.length;

  for (let i = 0; i <= lenArr - 1; i++) {
    if (lenArr === 0) {
      break;
    }

    if (arr[i] > 0) {
      sum = sum + arr[i];
    } else {
      break;
    }
  }

  return sum;
}

let input = [];

runningSum(input);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For an empty array the for loop would not be executed and it will return 0. You can remove the if condition to check length of array inside for loop.

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!