[Xerte] indexOf and arrays

Julian Tenney Julian.Tenney at nottingham.ac.uk
Fri Oct 22 08:40:46 BST 2010


These are the additional methods. Randomize() is a handy one. Contains() is just faster than findValue(), but it just returns Boolean rather than an index.

Array.prototype.sum = function() {
                var sum = 0;
                for (var i = 0; i<this.length; i++) {
                                sum += this[i];
                }
                return sum;
};
Array.prototype.sumTo = function(index) {
                var sum = 0;
                for (var i = 0; i<index; i++) {
                                sum += this[i];
                }
                return sum;
};
Array.prototype.contains = function(searchValue) {
                var i = this.length;
                while (i--) {
                                if (this[i] == searchValue) {
                                                return true;
                                }
                }
                return false;
};
Array.prototype.findValue = function(val) {
                var output = [];
                for (var i = 0; i<this.length; i++) {
                                if (this[i] == val) {
                                                return i;
                                }
                }
                return -1;
};
Array.prototype.deleteAtIndex = function(index) {
                this.splice(index, 1);
};
Array.prototype.randomize = function() {
                return this.sort(function (a, b) {
                                return (Math.floor(Math.random()*2) == 0) ? 1 : -1;
                });
};
Array.prototype.intersect = function(arr1, arr2) {
                var iSectArray:Array = new Array();
                var iSectObject:Object = new Object();
                for (var e1 = 0; e1<arr1.length; e1++) {
                                for (var e2 = 0; e2<arr2.length; e2++) {
                                                if (arr1[e1] == arr2[e2]) {
                                                                iSectObject[arr1[e1]] = "";
                                                }
                                }
                }
                for (var elem in iSectObject) {
                                iSectArray.push(elem);
                }
                iSectArray.sort();
                return iSectArray;
};

From: xerte-bounces at lists.nottingham.ac.uk [mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Dave Burnett
Sent: 21 October 2010 18:23
To: Xerte list
Subject: RE: [Xerte] indexOf and arrays

Cool.

Case sensitive?

________________________________
From: Julian.Tenney at nottingham.ac.uk
To: xerte at lists.nottingham.ac.uk
Date: Thu, 21 Oct 2010 18:05:58 +0100
Subject: RE: [Xerte] indexOf and arrays
check the help: I added a load of Authorware style methods to the array class for findValue, contains, etc

________________________________
From: xerte-bounces at lists.nottingham.ac.uk [xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Dave Burnett [d_b_burnett at hotmail.com]
Sent: 21 October 2010 17:52
To: Xerte list
Subject: RE: [Xerte] indexOf and arrays
Found it.

I thought an array of strings would be treated as such, but apparently not. :-)

attributeArray.toString().indexOf('graphic');

________________________________
From: Patrick.Lockley at nottingham.ac.uk
To: xerte at lists.nottingham.ac.uk
Date: Thu, 21 Oct 2010 17:38:17 +0100
Subject: RE: [Xerte] indexOf and arrays
Can only suggest searching the code, or waiting for Julian.

From: xerte-bounces at lists.nottingham.ac.uk [mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Dave Burnett
Sent: 21 October 2010 17:31
To: Xerte list
Subject: [Xerte] indexOf and arrays


Can I check for an array value

I debug my array and get this:

graphic,text,index

But,

debug(attributeArray.indexOf('graphic')); //returns undefined.

indexOf not supported or my syntax stinks?

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 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/20101022/a0bea94f/attachment.html


More information about the Xerte mailing list