Write a function named reverseReduce
that will be used to extend the Array prototype with a custom implementation of the reduce
method.
This function will behave similarly with the built-in reduce
method, but it will iterate over the elements starting from the end of the array to the beginning.
Array.prototype.reverseReduce = reverseReduce;
const names = ["Bob", "Jon", "Alex", "Sammy", "Diana"];
const concatNames = names.reverseReduce(
(accumulator, currentValue, currentIndex) => {
return accumulator + ` ${currentValue} on index ${currentIndex};`;
},
""
);
console.log(concatNames);
// Should print:
// Diana on index 4; Sammy on index 3; Alex on index 2; Jon on index 1; Bob on index 0;