inktherapy Posted January 15, 2009 Share Posted January 15, 2009 Hi,I want to add numerical values inside an array and display the total?e.g. x[0] = 2;x[1] = 1;x[2] = 4;total is x=7.Thanks. Link to comment Share on other sites More sharing options...
Synook Posted January 15, 2009 Share Posted January 15, 2009 You could write a custom method - I don't think there is anything predefined for this. Array.prototype.total = function() { var value = 0; for (i = 0; i < this.length; i++) { value += this[i]; } return value;} Then call it: var x = [2, 1, 4];alert(x.total()); //alerts 7 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.