Array.sum returns NaN if a array item is undefined. #1114

Closed
mientjan opened this Issue Mar 27, 2012 · 2 comments

Comments

Projects
None yet
3 participants
@mientjan

Array.sum returns NaN if a array item is undefined. this issue is found MooTools More 1.4.0.1

the solution is to check if a item is available.

Array.prototype.sum = function(){
    var r = 0;
    for( var i = 0, l = this.length; i < l; ++i ){
        if(this[i]){ r += this[i]; }
    }
    return r;
}
@arian

This comment has been minimized.

Show comment Hide comment
@arian

arian Mar 27, 2012

Member

agreed, or just use .forEach, which already skips undefined values: https://github.com/mootools/mootools-core/blob/master/Source/Core/Core.js#L317-321

Member

arian commented Mar 27, 2012

agreed, or just use .forEach, which already skips undefined values: https://github.com/mootools/mootools-core/blob/master/Source/Core/Core.js#L317-321

@mientjan

This comment has been minimized.

Show comment Hide comment
@mientjan

mientjan Mar 27, 2012

Yea, that is even better. The code i provided would not work with negative values any way.

here is the fix for both ways to do it.

// i prefer this way because no new function has to be created. 
Array.prototype.sum = function(){
    var r = 0;
    for( var i = 0, l = this.length; i < l; ++i ){
        if( i in this ){ r += this[i]; }
    }
    return r;
}

Array.prototype.sum = function(){
    var r = 0;
    this.forEach(function(n){ r += n; });
    return r;
}

Yea, that is even better. The code i provided would not work with negative values any way.

here is the fix for both ways to do it.

// i prefer this way because no new function has to be created. 
Array.prototype.sum = function(){
    var r = 0;
    for( var i = 0, l = this.length; i < l; ++i ){
        if( i in this ){ r += this[i]; }
    }
    return r;
}

Array.prototype.sum = function(){
    var r = 0;
    this.forEach(function(n){ r += n; });
    return r;
}

@SergioCrisostomo SergioCrisostomo added the bug label May 2, 2014

SergioCrisostomo added a commit to SergioCrisostomo/mootools-more that referenced this issue May 2, 2014

Make Array.sum() tolerate undefined elements
Array.sum returns `NaN` if a array item is undefined.

[10, 20, undefined, 30].sum(); // "NaN"

This PR makes:
-  `Array.sum()` ignore `undefined` values
- more explicit in the docs that the method will return NaN if a
*non-number* is present in the array
- adds extra specs

##### PS: - All this is @timwienk 's credit & work!

fixes  #1114

SergioCrisostomo added a commit that referenced this issue May 3, 2014

Merge: make Array.sum() ignore undefined values
Array.sum returns `NaN` if a array item is undefined.

    [10, 20, undefined, 30].sum(); // "NaN"

This PR makes:
-  `Array.sum()` ignore `undefined` values
- more explicit in the docs that the method will return NaN if a
*non-number* is present in the array
- adds extra specs

fixes  #1114
closes #1258
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment