可以幫幫我嗎!中間該填什麼⋯⋯
/*
"use strict";
let partition = function (test, ary){
......
}
let condSumArray = function(f, ary) {
......
}
......
console.log(condSumArray(function(n) {return n % 2 == 0 ? true : false}, []));
console.log(condSumArray(function(n) {return n % 2 == 0 ? true : false}, [1, 2, 5, 8, 9]));
// 將奇數、偶數分兩組後,計算總和:[[1, 5, 9] [2, 8]] => [15, 10]
console.log(condSumArray(function(n) {return n >= 0 ? true : false}, [-1, 3, -4, 2, 5]));
// 將負數、正數分兩組後,計算總和:[[-1, -4] [3, 2, 5]] => [-5, 10]
*/
試試看
let partition = function (test, ary){
return ary.filter(test);
}
let condSumArray = function(f, ary) {
if (ary.length==0) { return []; }
arrTwo = partition(f, ary);
arrOne = ary.filter(function(i) {return arrTwo.indexOf(i) < 0;});
return [arrOne.reduce((a,b)=>a+b), arrTwo.reduce((a,b)=>a+b)];
}
console.log(condSumArray(function(n) {return n % 2 == 0 ? true : false}, []));
console.log(condSumArray(function(n) {return n % 2 == 0 ? true : false}, [1, 2, 5, 8, 9]));
console.log(condSumArray(function(n) {return n >= 0 ? true : false}, [-1, 3, -4, 2, 5]));