Javascript Array reduce(func)

Description

Javascript Array reduce(func)



function add(a,b){
    return a+b;//from  w w w  .j  a v a  2 s.co  m
};



Array.prototype.reduce = function(func){
    var self = this;
    var count;
    var value = 0;
    for (count = self.length-1; count > -1; count--) {
        value = func(self[count],value);
    }

    console.log(value);
    return value;
}

var numArray = [0,1,2,3,4];
numArray.reduce(add);



PreviousNext

Related