Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of all elements in an array

I am a beginner in programming. I want to do the sum of all elements in an array. I made this but I can't see where are my mistakes?

function ArrayAdder(_array) {
    this.sum = 0;
    this.array = _array || [];
}

ArrayAdder.prototype.computeTotal = function () {
    this.sum = 0;
    this.array.forEach(function (value) {
        this.sum += value;
    });
    return this.sum;
};

var myArray = new ArrayAdder([1, 2, 3]);
console.log(myArray.computeTotal());
like image 277
stevielan Avatar asked Jan 16 '16 18:01

stevielan


People also ask

How to find sum of all elements in an array in C?

How to write a C Program to find Sum of all Elements in an Array using For Loop, While Loop, Functions with example. This C program allows the user to enter the Size, and number of rows for One Dimensional Arrays. Next, we are using the For Loop to iterate the array values and perform addition.

How do you find the sum of a multidimensional array?

If A is a multidimensional array, then sum (A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors. This dimension becomes 1 while the sizes of all other dimensions remain the same. S = sum (A,'all') computes the sum of all elements of A.

What does s = sum (a) mean in math?

Description. S = sum(A,vecdim) sums the elements of A based on the dimensions specified in the vector vecdim. For example, if A is a matrix, then sum(A,[1 2]) is the sum of all elements in A, since every element of a matrix is contained in the array slice...

How do you sum values in an array in Python?

Set the value of sum=0. Start a for loop from index 0 to the length of the array – 1. In every iteration, perform sum = sum + arr [i]. After the termination of the loop, print the value of the sum.


Video Answer


2 Answers

this inside the forEach callback refers to the global window object. To set the context of the callback, use the Array#forEach second argument to pass the context.

this.array.forEach(function (value) {
    this.sum += value;
}, this); // <-- `this` is bound to the `forEach` callback.

function ArrayAdder(_array) {
    this.sum = 0;
    this.array = _array || [];
}

ArrayAdder.prototype.computeTotal = function () {
    this.sum = 0;
    this.array.forEach(function (value) {
        this.sum += value;
    }, this);
    return this.sum;
};

var myArray = new ArrayAdder([1, 2, 3]);

console.log(myArray.computeTotal());
document.write(myArray.computeTotal()); // For Demo purpose

If you're looking for an alternative, you can use Array#reduce, here using with Arrow function

var sum = arr.reduce((x, y) => x + y);

// Note: If this doesn't work in your browser,
// check in the latest version of Chrome/Firefox

var arr = [1, 2, 3];
var sum = arr.reduce((x, y) => x + y);

document.write(sum);
like image 158
Tushar Avatar answered Oct 18 '22 11:10

Tushar


Reference of this got changed in forEach function. Update your code to following

function ArrayAdder(_array) {
  this.sum = 0;
  this.array = _array || [];
}

ArrayAdder.prototype.computeTotal = function() {
  this.sum = 0;
  var that = this;
  this.array.forEach(function(value) {
    that.sum += value;
  });
  return this.sum;
};

var myArray = new ArrayAdder([1, 2, 3]);
console.log(myArray.computeTotal());

The above saved the reference of this in that and used it for computing.

like image 35
Nikhil Aggarwal Avatar answered Oct 18 '22 10:10

Nikhil Aggarwal