startit/eloquentjavascript/05_higher_order_functions/summarizing_with_reduce.js

20 lines
548 B
JavaScript

const SCRIPTS = require("./scripts.js");
function reduce(array, combine, start) {
let current = start;
for(let element of array){
current = combine(current, element);
}
return current
}
console.log(reduce([1,2,3,4], (a,b) => a +b, 0));
console.log([1,2,3,4].reduce((a,b) => a +b));
function characterCount(script) {
return script.ranges.reduce((count, [from, to]) => {
return count + (to - from);
}, 0);
}
console.log(SCRIPTS.reduce((a,b) => {
return characterCount(a) < characterCount(b) ? b : a;
}));