Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a JavaScript or jQuery equivalent to Python's "sum" built-in function?

Say I have an array-ish container of decimal numbers. I want the sum. In Python I would do this:

x = [1.2, 3.4, 5.6]

sum(x)

Is there a similarly concise way to do this in JavaScript?

like image 916
twneale Avatar asked Jul 30 '10 01:07

twneale


People also ask

What can I use instead of sum in Python?

If your code is constantly summing floating-point numbers with sum() , then you should consider using math. fsum() instead.

How do you sum numbers in JavaScript?

const num1 = parseInt(prompt('Enter the first number ')); const num2 = parseInt(prompt('Enter the second number ')); Then, the sum of the numbers is computed. const sum = num1 + num2; Finally, the sum is displayed.

Is there any sum function in JavaScript?

sum() function in D3. js is used to return the sum of the given array's elements. If the array is empty then it returns 0. Parameters: This function accepts a parameters Array which is an array of elements whose sum are to be calculated.

Does Python have a built-in sum function?

sum() function in PythonPython provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.


2 Answers

Another approach, a simple iterative function:

function sum(arr) {
  var result = 0, n = arr.length || 0; //may use >>> 0 to ensure length is Uint32
  while(n--) {
    result += +arr[n]; // unary operator to ensure ToNumber conversion
  }
  return result;
}

var x = [1.2, 3.4, 5.6];
sum(x); // 10.2

Yet another approach using Array.prototype.reduce:

var arr = [1.2, 3.4, 5.6];
arr.reduce(function (a, b) { return a + b; }, 0); // 10.2

The reduce method is part of the ECMAScript 5th Edition standard, is widely available, but is not on IE <= 8, however an implementation cay be included from the the Mozilla Dev Center I linked.

like image 95
Christian C. Salvadó Avatar answered Oct 28 '22 09:10

Christian C. Salvadó


I guess there's none... but you can make one on javascript

Array.prototype.sum = function() {
  return (! this.length) ? 0 : this.slice(1).sum() +
      ((typeof this[0] == 'number') ? this[0] : 0);
};

use it as,

[1,2,3,4,5].sum() //--> returns 15
[1,2,'',3,''].sum() //--> returns 6
[].sum() //--> returns 0    
x = [1.2, 3.4, 5.6]
x.sum(); // returns 10.2

demo


Okay as pointed out in the comment, you can also do it as non-recursive way

Array.prototype.sum = function() {
   var num = 0;
   for (var i = 0; i < this.length; i++) {
       num += (typeof this[i] == 'number') ? this[i] : 0;
   }
   return num;
};

Another way to do it, through function..

function sum(arr) {
   var num = 0;
   for (var i = 0; i < arr.length; i++) {
       num += (typeof arr[i] == 'number') ? arr[i] : 0;
   }
   return num;
};

use it as,

sum([1,2,3,4,5]) //--> returns 15
x = [1.2, 3.4, 5.6]
sum(x); // returns 10.2
like image 39
Reigel Avatar answered Oct 28 '22 09:10

Reigel