daily pastebin goal
15%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top