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

187
Views
Find Sum of Array Values up to Index

I have an array like this:

var array = [0, 4, 6, 6, 6, 6, 6, 6];

I want a function to find the sum of the arrays values up to a certain index.

e.g. up to index 4 will be 0 + 4 + 6 + 6 + 6 = 22

Here's what I have so far:

var array = [0, 4, 6, 6, 6, 6, 6, 6];
const index = 4;
function sumUpToIndex(array, index) {
    var result = 0;

    for(i = 0; i < index; i++) {
        result += array[i];
    }

    return result;
}
console.log(sumUpToIndex(array, index));

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

e.g. up to index 4 will be 0 + 4 + 6 + 6 + 6 = 22

Is a little mistake. you mean a) 4 = 16 or 5 = 22

working example where you can check

const i = document.querySelector('input')

i.addEventListener('keyup', (e) => {
  const to = e.target.value; 
  const array = [0, 4, 6, 6, 6, 6, 6, 6].slice(0, to);
  const sum = array.reduce((accumulator, a) => {
    return accumulator + a;
   }, 0);
   console.log(sum);
})
<input type="number">

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!