Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda