let foo = 0;
let bar = 0;
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [6, 7, 8, 9, 10];
function calculateSum(arr) {
return arr.reduce((accum, val) => accum + val, 0);
}
foo = calculateSum(arr1); // Expect foo to equal 15
bar = calculateSum(arr2); // Expect bar to equal 40
I want to reuse this function for different arrays and values. Instead of passing the sum as an argument, let's update the value directly by returning it from the function.
The new syntax maintains clarity in the code while achieving the desired functionality.