3 ms·
Next iteration looks like this. Note that I added another test when I made the update to the function. function reduceRight(arr, value, reduceFunc) {
by z3t4 7y ago
Next iteration looks like this. Note that I added another test when I made the update to the function.
function reduceRight(arr, value, reduceFunc) {
if(typeof value == "function" && reduceFunc == undefined) {
reduceFunc = value;
value = arr.pop();
}
for(var i=arr.length; i--; i<-1) {
value = reduceFunc(value, arr[i]);
}
return value;
}
assert( reduceRight([1,2,3], (a,b)=>a+b) , 6);
assert( reduceRight([1,2,3], 1, (a,b)=>a+b) , 7);
assert( reduceRight([1,2,3], (a,b)=>a-b) , 0);