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());
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.
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.
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...
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With