// Тут 12 случайных чисел
const numbers = [0, 1, 5, 0, 4, 1, 3, 6, 0, 1, 5, 4]
0 + 1 = 1, 5 + 0 = 5, 4 + 1 = 5
// И массив становится таким, где уже 6 чисел:
numbers = [1, 5, 5, 9, 1, 9]
const result = arr.reduce((acc, n, i) => (
(i & 1) || acc.push(0),
acc[~-acc.length] += n,
acc
), []);
function chunked(data, chunkSize) {
return Array.prototype.reduce.call(
data,
(acc, n, i) => ((acc[i / chunkSize | 0] ??= []).push(n), acc),
[]
);
}
function sum(data, val = n => n) {
const getVal = val instanceof Function ? val : n => n[val];
return Array.prototype.reduce.call(
data,
(acc, n) => acc + getVal(n),
0
);
}
const result = chunked(arr, 2).map(n => sum(n));