I want the end result to display "276 + 351 = 627". Is there a more efficient way to do so while using for loops? This is a practice question I found online, so the stuff after the 2 arrays is what I've come up with.
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 sum1 = 0;
let sum2 = 0;
for (let i = 0; i < arr_1.length; i++) {
sum1 += arr_1[i];
}
for (let j = 0; j < arr_2.length; j++) {
sum2 += arr_2[j];
}
let total = sum1 + sum2;
console.log(`${sum1} + ${sum2} = ${total}`);
The reduce
function allows you to write it in a very short manner:
const arr_1 = [3, 5, 22, 5, 7, 2, 45, 75, 89, 21, 2], arr_2 = [9, 2, 42, 55, 71, 22, 4, 5, 90, 25, 26], add = arr => arr.reduce((sum, n) => sum + n), sum1 = add(arr_1), sum2 = add(arr_2), total = sum1 + sum2; console.log(`${sum1} + ${sum2} = ${total}`);
There many way to loop through a array if you read the MDN Docs Link Here . One of the way is reduce() which work with almost all browsers but i would prefer to use forEach() as it is one of oldest proto on arrays.. and here is how it is done
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 sum1 = 0
let sum2 = 0
let total = 0
arr_1.forEach((el) => {
sum1 += el
});
arr_2.forEach((el) => {
sum2 += el
});
total = sum1 + sum2
console.log(`${sum1} + ${sum2} = ${total}`);
JSFiddle Link
this way
const doSum = arr => arr.reduce((a,c)=>a+c,0) // let zero value, if arr is empty, arr_1 = [ 3, 5, 22, 5, 7, 2, 45, 75, 89, 21, 2 ], arr_2 = [ 9, 2, 42, 55, 71, 22, 4, 5, 90, 25, 26 ], sum1 = doSum(arr_1), sum2 = doSum(arr_2), total = sum1 + sum2; console.log(`${sum1} + ${sum2} = ${total}`)
Assuming both arr_1
and arr_2
have the same length, you only need one loop. This is as efficient (as in speed/performance) as you'll get:
let sum1 = 0, sum2 = 0,
arr_1 = [3, 5, 22, 5, 7, 2, 45, 75, 89, 21, 2],
arr_2 = [9, 2, 42, 55, 71, 22, 4, 5, 90, 25, 26];
for (let i = 0; i < arr_1.length; i++) {
sum1 += arr_1[i], sum2 += arr_2[i];
}
let total = sum1 + sum2;
console.log(`${sum1} + ${sum2} = ${total}`);
Plain for
loops are typically faster than more high level looping functions, so you already have the best performing loop possible.
You could also use this for arrays of different lengths by looping the larger one, still only doing one for
loop:
let sum1 = 0, sum2 = 0,
arr_1 = [3, 5, 22, 5, 7, 2, 45, 75, 89, 21, 2, 6, 40, 4],
arr_2 = [9, 2, 42, 55, 71, 22, 4, 5, 90, 25, 26];
for (let i = 0; i < arr_1.length; i++) {
sum1 += arr_1[i], sum2 += arr_2[i] || 0;
}
let total = sum1 + sum2;
console.log(`${sum1} + ${sum2} = ${total}`);
When arr_2
runs out, it will start outputting undefined
. The arr_2[i] || 0
arr_2[i] || 0
expression will use the secondary value in that case.
An alternative would be to check if the offset exists:
for (let i = 0; i < arr_1.length; i++) {
sum1 += arr_1[i];
if(arr_2[i]) sum2 += arr_2[i];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.