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

155
Views
Why do my Function Parameters Increase when function is called again?

Hello I am quite confused on this problem. I have a pretty simple function that changes the values of my parameters although i don't want this behavior. So when I call the function I want it to output the same values every time but instead its output is increasing. What am I doing wrong and especially why (I have more of a python background).

function calcSumHist(arr) {
    const sum = []
    for (const hist of arr) {
        for (const entry of hist) {
            const index = sum.findIndex(x => x[0] === entry[0])
            index === -1 ? sum.push(entry) : sum[index][1] += entry[1]
        }
    }
    return sum
}

const myHist1 = [["a", 1000], ["b", 1000], ["c", 1000]]

const myHist2 = [["a", 500], ["b", 500], ["c", 500]]

function getResult(arr) {
    const result = calcSumHist([myHist1, myHist2])
    return result
}

console.log(getResult([myHist1, myHist2]));
console.log(getResult([myHist1, myHist2]));
console.log(getResult([myHist1, myHist2]));
.as-console-wrapper {
    max-height: 100% !important;
}

Outputs this:

> Array [Array ["a", 1500], Array ["b", 1500], Array ["c", 1500]]
> Array [Array ["a", 2000], Array ["b", 2000], Array ["c", 2000]]
> Array [Array ["a", 2500], Array ["b", 2500], Array ["c", 2500]]

But I want this:

> Array [Array ["a", 1500], Array ["b", 1500], Array ["c", 1500]]
> Array [Array ["a", 1500], Array ["b", 1500], Array ["c", 1500]]
> Array [Array ["a", 1500], Array ["b", 1500], Array ["c", 1500]]
about 4 years ago · Juan Pablo Isaza
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!