最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sum of all elements in an array - Stack Overflow

programmeradmin3浏览0评论

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.prototypeputeTotal = function () {
    this.sum = 0;
    this.array.forEach(function (value) {
        this.sum += value;
    });
    return this.sum;
};

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

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());
Share Improve this question edited Jan 16, 2016 at 18:16 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Jan 16, 2016 at 18:05 stevielanstevielan 551 gold badge1 silver badge4 bronze badges 1
  • Related: stackoverflow.com/questions/1230233/… – nicael Commented Jan 16, 2016 at 18:10
Add a comment  | 

4 Answers 4

Reset to default 12

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.

The most effective way of doing this is to use the reduce array function. For example:

this.array = [0, 1, 2, 3]
this.sum = this.array.reduce(function(a, b) {
  return a + b;
});

Try this:

  let arr = [1, 2, 3, 4, 5];
  const sum = arr.reduce((x, y)=> x+y);
  console.log(sum);

发布评论

评论列表(0)

  1. 暂无评论