This is an exercise and a challenge to see how far can I go with reduce and how many built-in Array methods I can replicate with it.
-
I use ES2015 arrow functions in all the examples below, that's because arrow functions make the code easier to read and in most cases turn the code snippet into a bite-sized one.
-
The first part of the code is used to generate random values for the reduce function to work with:
var arr = new Array(100)
.fill('')
.map(() => Math.floor(Math.random() * 10))
So for example if we want to use concat, we will do the following:
// define the arr array which contains our sample
var arr = new Array(100)
.fill('')
.map(() => Math.floor(Math.random() * 10));
// definition the concat function utilizing reduce
var concat = (arr, arr2) =>
arr2.reduce((memo, current) => {
return memo.push(current) && memo;
}, arr);
// and then use concat on arr and another array as the 2nd parameter of concat
concat(arr, [1,2,3]);