function balance(arr1, arr2) {
let all = arr1.concat(arr2);
all.sort((a, b) => a - b);
arr1 = []; arr2 = [];
let sum1 = 0, sum2 = 0, elem;
while ((elem = all.pop()) !== undefined) {
if (sum1 > sum2) {
arr2.push(elem);
sum2 += elem;
} else {
arr1.push(elem);
sum1 += elem;
}
}
return {
arr1: arr1,
arr2: arr2,
sum1: sum1,
sum2: sum2,
}
}
var arr1 = [10, 300, 25, 75];
var arr2 = [50, 125, 500, 10];
balance(arr1, arr2);
function makeCounter() {
let count = 0;
let f = function() {
return count++; // есть доступ к внешней переменной "count"
};
return f;
}