Ad
Code
Diff
  • /* Array.reduce(
     * (accumulator, currentValue, currentIndex, array) => {
     * return accumulator + currentValue;
     * },
     * initialValue
     * );
     */
    function sumOfElements(arr) { 
      return (arr||[]).reduce((sum, currentValue) => sum + currentValue,0);
    }
    
    // Using ES6 Arrow Functions
    sumOfElements = arr => (arr||[]).reduce((sum, currentValue) => sum + currentValue,0)
    • /* Array.reduce(
    • * (accumulator, currentValue, currentIndex, array) => {
    • * return accumulator + currentValue;
    • * },
    • * initialValue
    • * );
    • */
    • function sumOfElements(arr) {
    • if(arr==undefined) return 0;
    • return arr.reduce((sum, currentValue) => sum + currentValue,0);
    • }
    • return (arr||[]).reduce((sum, currentValue) => sum + currentValue,0);
    • }
    • // Using ES6 Arrow Functions
    • sumOfElements = arr => (arr||[]).reduce((sum, currentValue) => sum + currentValue,0)