JavaScript equivalent to Python's "sum" built-in function - Javascript Array

Javascript examples for Array:slice

Description

JavaScript equivalent to Python's "sum" built-in function

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <title>Reigel's demo page</title> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.4.2.js"></script> 
  <script type="text/javascript">
Array.prototype.sum = function() {
  return (! this.length) ? 0 : this.slice(1).sum() +
      ((typeof this[0] == 'number') ? this[0] : 0);
};
x = [1.2, 3.4, 5.6]//from w  w w. j a  va 2 s  . c om
console.log(x.sum());

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

Related Tutorials