'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]]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
