Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. function reduce(list, callback, initialValue) {
  2. if (list == null || !Array.isArray(list)) {
  3. throw new TypeError("list is not a valid array");
  4. }
  5. if (typeof callback !== "function") {
  6. throw new TypeError(callback + " is not a function");
  7. }
  8. if (initialValue === undefined && list.length === 0) {
  9. throw new TypeError("Reduce of empty array with no initial value");
  10. }
  11.  
  12. let accumulator = initialValue;
  13. for (let currentIndex = 0; currentIndex < list.length; currentIndex++) {
  14. currentValue = list[currentIndex];
  15. accumulator = callback(accumulator, currentValue, currentIndex, list);
  16. }
  17. return accumulator;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement