Is there a more elegant/better way to code this loop exercise?

Get the sum of the two arrays
let arr_1 = [3, 5, 22, 5, 7, 2, 45, 75, 89, 21, 2];
let arr_2 = [9, 2, 42, 55, 71, 22, 4, 5, 90, 25, 26];

My attempt

let total1 = 0;
let total2 = 0;

for( let i = 0; i < arr_1.length; i++){
total1+= arr_1[i];

} for ( let j = 0; j < arr_2.length; j++){
total2+=arr_2[j];

}

let arrTotal = total1 + total2;

console.log(arrTotal);

I just want to learn more or better methods of solving this loop excercise .

Yes you could use reduce to sum the total

let arr_1 = [3, 5, 22, 5, 7, 2, 45, 75, 89, 21, 2]; let arr_2 = [9, 2, 42, 55, 71, 22, 4, 5, 90, 25, 26]; let total = [].concat(arr_1, arr_2).reduce((acc, curr) => acc + curr); console.log(total);
2 Likes