Open this example in the interactive library
Source code
const totals = [0];
for (const amount of [5, 3, 8, 2]) totals.push(totals.at(-1) + amount);
console.log(totals[4] - totals[1]);
Sample output
13

Open this example in the interactive library
const totals = [0];
for (const amount of [5, 3, 8, 2]) totals.push(totals.at(-1) + amount);
console.log(totals[4] - totals[1]);
13