I'm new to programming and I'm trying to calculate the sum of all elements in an array. I've written this code, but it doesn't seem to be working properly. Can someone help me find 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 numbersArray = new ArrayAdder([1, 2, 3]);
console.log(numbersArray.computeTotal());