JSFiddle - React, Tailwind, and code Playground
JavaScript
Array.prototype.sum = function(predicate){
predicate = predicate || function(match){
if(typeof match =="string"){
return match.length;
}
return match;
};
var result = 0;
for (var i = 0; i < this.length; i++) {
result += predicate(this[i]);
}
return result;
};
var array1 = ["hello","world"];
console.log(array1.sum());
var array2 = [10,20,30];
console.log(array2.sum());