[Xerte] Re: sum part of array?

Dave Burnett d_b_burnett at hotmail.com
Wed Jul 27 13:50:06 BST 2011




Thanks Julian.
Handy.



From: Julian.Tenney at nottingham.ac.uk
To: xerte at lists.nottingham.ac.uk
Date: Wed, 27 Jul 2011 13:39:39 +0100
Subject: [Xerte] Re: sum part of array?

I did it like this so you can pass one param for sum from 0th to nth item, or n,m for the a sum of the range between n an m, or no params for sum of it all:
 
Array.prototype.sum = function(n,m) {
	if (arguments.length == 1){
		return this.slice(0,n).sum()
	} 
	if (arguments.length == 2){
		return this.slice(n,m).sum()
	}
 
	var sum = 0;
	for (var i = 0; i<this.length; i++) {
		sum += this[i];
	}
	return sum;
};
 
-----Original Message-----
From: xerte-bounces at lists.nottingham.ac.uk [mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Julian Tenney
Sent: 26 July 2011 16:18
To: Xerte discussion list
Subject: [Xerte] Re: sum part of array?
 
myArray.slice(n,m).sum();
 
//test code/////////////////////////////////
Array.prototype.sum = function() {
	var sum = 0;
	for (var i = 0; i<this.length; i++) {
		sum += this[i];
	}
	return sum;
};
 
myArray = [0,1,2,3,4,5,6];
 
trace(myArray.slice(1,1).sum()); //n and m are 1 based, not zero??
 
 
-----Original Message-----
From: xerte-bounces at lists.nottingham.ac.uk [mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Pat Lockley
Sent: 26 July 2011 15:30
To: Xerte discussion list
Subject: [Xerte] Re: sum part of array?
 
eval(array.join("+")) ?
 
On Tue, Jul 26, 2011 at 2:50 PM, Dave Burnett <d_b_burnett at hotmail.com> wrote:
>
>
> Just out of curiosity, is there any way to sum part of an array?
>
>
> [1,2,3,4,5]
>
>
> Lets say I have an index variable and I always will want to sum from 0 to
> that variable.
>
>
> pseudo
> my_array.sum(0,var);
>
>
> Or am I forced to loop + add?
>
>
> Or any elegant way to create a temp array of the target elements and sum on
> that instead?
>
>
> Dave
>
>
>
>
>
> This message and any attachment are intended solely for the addressee and
> may contain confidential information. If you have received this message in
> error, please send it back to me, and immediately delete it. Please do not
> use, copy or disclose the information contained in this message or in any
> attachment. Any views or opinions expressed by the author of this email do
> not necessarily reflect the views of the University of Nottingham.
>
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses which could damage your computer system:
> you are advised to perform your own checks. Email communications with the
> University of Nottingham may be monitored as permitted by UK legislation.
>
> _______________________________________________
> Xerte mailing list
> Xerte at lists.nottingham.ac.uk
> http://lists.nottingham.ac.uk/mailman/listinfo/xerte
>
> This message and any attachment are intended solely for the addressee and
> may contain confidential information. If you have received this message in
> error, please send it back to me, and immediately delete it.   Please do not
> use, copy or disclose the information contained in this message or in any
> attachment.  Any views or opinions expressed by the author of this email do
> not necessarily reflect the views of the University of Nottingham.
>
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses which could damage your computer system:
> you are advised to perform your own checks. Email communications with the
> University of Nottingham may be monitored as permitted by UK legislation.
>
>
>
 
_______________________________________________
Xerte mailing list
Xerte at lists.nottingham.ac.uk
http://lists.nottingham.ac.uk/mailman/listinfo/xerte
 
_______________________________________________
Xerte mailing list
Xerte at lists.nottingham.ac.uk
http://lists.nottingham.ac.uk/mailman/listinfo/xerte

_______________________________________________
Xerte mailing list
Xerte at lists.nottingham.ac.uk
http://lists.nottingham.ac.uk/mailman/listinfo/xerte

This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.   Please do not use, copy or disclose the information contained in this message or in any attachment.  Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation. 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nottingham.ac.uk/pipermail/xerte/attachments/20110727/45d273d1/attachment.html>


More information about the Xerte mailing list