In ES6 you can use spread operator. One string solution:
Copy Math.min(...items)
Answer from Sergey Zhukov on Stack Overflowjavascript - Using reduce() to find min and max values? - Stack Overflow
javascript - How do I write my own `reduce` function? - Stack Overflow
Animal advocates should be cautious of cost-effectiveness — EA Forum
Mastering the reduce function
Hey so I wrote this article and 100% transparency - I'm a new writer. I do a lot of front-end work and I'm just trying to post relevant content. I had trouble when I started learning the functional methods within Array.prototype. I thought this would be useful to this sub. This seems to fit the subreddit's guidelines and it was downvoted into oblivion.
If you downvoted, and don't mind sharing, could you tell me why? I just want to get better, and I'm sort of scratching my head here trying to figure out what went wrong. I had quite a few friends review this article (work friends as well as in JS/front-end communities) before I posted it, and the sentiment was generally positive.
More on reddit.comVideos
I own searched online but I do not understandthings like accumalator or reducer, I understand .map and .filter but not .reduce. If you could an analogy would be appreciated.
In ES6 you can use spread operator. One string solution:
Copy Math.min(...items)
The trick consist in provide an empty Array as initialValue Parameter
Copyarr.reduce(callback, [initialValue])
initialValue [Optional] Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.
So the code would look like this:
Copyfunction minMax(items) {
return items.reduce((acc, val) => {
acc[0] = ( acc[0] === undefined || val < acc[0] ) ? val : acc[0]
acc[1] = ( acc[1] === undefined || val > acc[1] ) ? val : acc[1]
return acc;
}, []);
}
The array in subject is not passed as argument, but is the context (this).
You also need to distinguish between the presence or absence of the start value:
var a = [10, 21, 13, 56];
function add(a, b) { return a + b }
function foo(a, b) { return a.concat(b) }
Array.prototype.reduce2 = function (f, result) {
var i = 0;
if (arguments.length < 2) {
i = 1;
result = this[0];
}
for(; i < this.length; i++) {
result = f(result, this[i], i, this);
}
return result;
};
console.log(a.reduce(add), a.reduce2(add)) // 100 100
console.log(a.reduce(add, 10), a.reduce2(add, 10)) // 110 110
// extra test with foo:
console.log(a.reduce(foo, 'X'), a.reduce2(foo, 'X')) // X10211356 X10211356
Based on your code
var a = [10, 21, 13, 56];
function add(a, b) { return a + b }
function foo(a, b) { return a.concat(b) }
Array.prototype.reduce2 = function(fn, start){
var result = start !== undefined ? start : this[0];
for (var i = 0; i < this.length; i++) {
result = fn(result, this[i]);
}
return result;
};
console.log(a.reduce(add), a.reduce2(add)) // 100 100
console.log(a.reduce(add, 10), a.reduce2(add, 10)) // 110 110
console.log(a.reduce(foo, ''), a.reduce2(foo, ''));
console.log(a.reduce(foo, 'X'), a.reduce2(foo, 'X'));