Code
Diff
  • /* 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);
    }
    • /* Array.reduce(
    • * (accumulator, currentValue, currentIndex, array) => {
    • * return accumulator + currentValue;
    • * },
    • * initialValue
    • * );
    • */
    • function sumOfElements(arr) {
    • return arr ? arr.reduce( (sum, currentValue) => sum + currentValue) : 0;
    • function sumOfElements(arr) {
    • if(arr==undefined) return 0;
    • return arr.reduce((sum, currentValue) => sum + currentValue,0);
    • }