Ad
Code
Diff
  • /* Array.reduce(
     * (accumulator, currentValue, currentIndex, array) => {
     * return accumulator + currentValue;
     * },
     * initialValue
     * );
     */
    function sumOfElements(arr) {    
      return arr ? arr.reduce( (sum, currentValue) => sum + currentValue) : 0;
    }
    • /* Array.reduce(
    • * (accumulator, currentValue, currentIndex, array) => {
    • * return accumulator + currentValue;
    • * },
    • * initialValue
    • * );
    • */
    • function sumOfElements(arr) {
    • if(arguments.length === 0)
    • arr = [1,2,3,4,5];
    • var total = arr.reduce( (sum, currentValue) => {return sum + currentValue}, 0 );
    • console.log(total);
    • return total;
    • function sumOfElements(arr) {
    • return arr ? arr.reduce( (sum, currentValue) => sum + currentValue) : 0;
    • }