Using Optional parameter in array reduce function - Javascript Array

Javascript examples for Array:reduce

Description

Using Optional parameter in array reduce function

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*  w w w  .j  a  v  a 2 s  .co  m*/
function reduce(arr, func, start) {
    if (arr.length == 0) {
        return start;
    }
    var accum = typeof start == 'undefined' ? arr[0] : func(start, arr[0]);
    return reduce(arr.slice(1), func, accum);
}
console.log(reduce([1,2,34,5,6,7,8,9], function(a,b){return a + b;}));
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials