187
rated 0 times
[
193]
[
6]
/ answers: 1 / hits: 25503
/ 9 Years ago, sat, january 16, 2016, 12:00:00
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());
More From » arrays