I have a toggle switch named "Today" and "Yesterday." There is an array of values that needs to be summed based on the selection. Clicking on "Today" should calculate the sum of values for the current day, while clicking on "Yesterday" should add today's and yesterday's values together.
A challenge I am currently facing is outlined below:
public val1;
public val2;
public val3;
public val4;
data = [];
if (value == 'a') {
val1 = arr.reduce((acc, cur) => acc + Number(cur)); // sum values in the array
} else if (value == 'b') {
val2 = arr.reduce((acc, cur) => acc + Number(cur));
} else if (value == 'c') {
val3 = arr.reduce((acc, cur) => acc + Number(cur));
} else if (value == 'd') {
val4 = arr.reduce((acc, cur) => acc + Number(cur));
}
this.data.push(this.val1, this.val2, this.val3, this.val4);
The issue arises when:
arr = [5, undefined, 5,5]
arr = [15,10,25,10]
Upon clicking "Today," it correctly displays [5, undefined, 5,5]
. When selecting "Yesterday," it shows [20,10,30,15]
(sum of today and yesterday). However, upon clicking on Today again, it displays [5, 10, 5,5]
, where the value of 10 is carried over from the previous array calculation but not the others. The desired output is [5, undefined, 5,5]
.