1function sumArray(a, b) {
2 var c = [];
3 for (var i = 0; i < Math.max(a.length, b.length); i++) {
4 c.push((a[i] || 0) + (b[i] || 0));
5 }
6 return c;
7}
1function sumArrays(...arrays) {
2 const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
3 const result = Array.from({ length: n });
4 return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
5}
6
7console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4
1function sumArray(a, b) {
2 var c = [];
3 for (var i = 0; i < Math.max(a.length, b.length); i++) {
4 c.push((a[i] || 0) + (b[i] || 0));
5 }
6 return c;
7}
8