[Xerte] Re: sum part of array?

Julian Tenney Julian.Tenney at nottingham.ac.uk
Tue Jul 26 16:18:01 BST 2011


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



More information about the Xerte mailing list