Nodejs Array Reduce reduceRight(fun /*, initial*/)

Here you can find the source of reduceRight(fun /*, initial*/)

Method Source Code

/**// www.  j av a 2 s .c o  m
 * Applies `fun` to `initial` and the last element of the array, and then to
 * the result and the second-to-last element, and so on.  Returns the result of
 * applying `fun` to the accumulated value and the first element of the array.
 *
 * `fun` is given four arguments:
 * <ol>
 * <li>the result of the previous invocation of `fun`, or the initial value on the first invocation</li>
 * <li>a single array element</li>
 * <li>the index of that element in the array</li>
 * <li>the original array</li>
 * </ol>
 *
 * This method behaves identically to Array#reduce except that it performs a
 * right-reduce instead of a left-reduce.
 *
 * This definition is compatible with the JavaScript 1.6 definition of
 * `Array#reduceRight` in Spidermonkey.
 *
 * This implementation comes from:
 * https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce
 *
 * @function
 * @param   {Function}  fun     function that will be applied to each array element
 * @param   {any}       [initial]   initial value; defaults to the last array element
 * @returns {any}       the return value from the last invocation of `fun`
 */
Array.prototype.reduceRight = function(fun /*, initial*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function") {
        throw new TypeError();
    }

    // no value to return if no initial value, empty array
    if (len == 0 && arguments.length == 1) {
        throw new TypeError();
    }

    var i = len - 1;
    if (arguments.length >= 2) {
        var rv = arguments[1];
    } else {
        do {
            if (i in this) {
                var rv = this[i--];
                break;
            }

            // if array contains no values, no initial value to return
            if (--i < 0) {
                throw new TypeError();
            }
        } while (true);
    }

    for (; i >= 0; i--) {
        if (i in this) {
            rv = fun.call(null, rv, this[i], i, this);
        }
    }

    return rv;
};

Related

  1. reduce(testFunction,initialValue)
    Array.prototype.reduce = function(testFunction,initialValue){
      var accumulatedValue,counter;
      if(this.length == 0){
        return this;
      }else{
        if(arguments.length == 1){
          counter = 1;
          accumulatedValue = this[0];
        }else if(arguments.length >= 2){
    ...
    
  2. reduce(testFunction,initialValue)
    Array.prototype.reduce = function(testFunction,initialValue){
      var accumulatedValue,counter;
      if(this.length == 0){
        return this;
      }else{
        if(arguments.length == 1){
          counter = 1;
          accumulatedValue = this[0];
        }else if(arguments.length >= 2){
    ...
    
  3. reduce(testFunction,initialValue)
    Array.prototype.reduce = function(testFunction,initialValue){
      var accumulatedValue,counter;
      if(this.length == 0){
        return this;
      }else{
        if(arguments.length == 1){
          counter = 1;
          accumulatedValue = this[0];
        }else if(arguments.length >= 2){
    ...
    
  4. reduce.call(str, (trav, char)>
    'use strict';
    class PrefixTree {
        constructor() {
            this.root = {};
        insert(str) {
            Array.prototype.reduce.call(str, (trav, char) => {
                if (!trav[char]) {
                    trav[char] = {};
    ...
    
  5. reduceRight(aggregate, initial)
    Array.prototype.reduceRight = function(aggregate, initial) {
      const len = this.length;
      let startIndex = len - 1;
      if(typeof initial === 'undefined') {
        initial = this[0];
        startIndex = len - 2;
      for(let i = startIndex; i >= 0; i -= 1) {
        initial = aggregate(initial, this[i], i, this);
    ...
    
  6. reduceRight2(pasteback, initial)
    Array.prototype.reduceRight2 = function(pasteback, initial){
      for(var i = this.length
          , value = arguments.length>1 ? initial : this[--i]
      ; i--;){
        value = pasteback(value, this[i], i, this);
      return value;
    };
    
  7. _reduce(f)
    Array.prototype._reduce = function(f){
        let acc = this[0];
        for(let i = 1; i < this.length; i++){
            acc = f(acc,this[i])
        return acc;