Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Created September 25, 2018 19:42
Show Gist options
  • Save ramsunvtech/dbc7a064048af001d3cf9a594cd2c11a to your computer and use it in GitHub Desktop.
Save ramsunvtech/dbc7a064048af001d3cf9a594cd2c11a to your computer and use it in GitHub Desktop.
Sum of Three Arrays
var a = [1,2,3,1,1];
var b = [4,5,6,1];
var c = [1,1,1];
function sumArray(a,b,c) {
const max = Math.max(a.length, b.length, c.length);
const sumList = [];
for(let i = 0; i < max; i++) {
sumList.push((a[i] || 0) + (b[i] || 0) + (c[i] || 0));
}
return sumList;
}
sumArray(a,b,c); // [6,8,10,2,1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment