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

160
Views
Trying to add each element of array to the next one for unknown number of elements

Something like this but generalized

let arr = [6, 5, 4, 3];
let i = 0, j = 0;
let result = [];

    result.push(arr[0]+arr[1],
arr[2]+arr[3]);

console.log(result
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ignoring last element if array has odd number of elements:

function f(arr) {
  let result = [];
  for (let i=0; i<arr.length; i++) {
     if (i % 2==0 && i<arr.length-1) {
        result.push(arr[i] + arr[i+1]);
        i++;
     }
  };
  return result;
}

let arr = [6, 5, 4, 3];

console.log(f(arr));
console.log(f([1, 2, 3, 4, 5, 6, 7]));

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!