Ask a Question related to Macromedia Flash, Design and Development.
-
oiv #1
I need help on Array object, (summing number elements)
i.e :
myArray = newArray()
myArray = [2,3,10];
sum = Number(myArray[0]+myArray[1]+myArray[2]);
trace (sum)
//traces 15
Anybody knows how to add number elementes of an array together dynamically?
Instead of typing myArray[0] + myArray[0] etc.
Řivind
oiv Guest
-
array elements matching object properties ?
I'm trying to sort out an application where I need create a conditional statement that matches elements of an array and properties of several... -
How to find the number of elements in an array
Ack -- I wanted to post this to both this group and alt.php but forgot to include this group in the original posting -- sorry. Forgive me if this... -
Number of elements in an array
I hope the subject isn't too misleading... I am trying to find the number of elements of each "data" array in the following: -- use... -
Accessing elements in array ref of array references
Currently I'm comparing the first value of each of the array references (which are stored in an array reference $ref_ref) as follows: ... -
Q- Empirical usable upper limit on hash array number of elements
carltonbrown@hotmail.com (Carlton Brown) wrote in message news:<aa611a32.0307090638.23f8297f@posting.google.com>... Hope it's not too much... -
Martin Voegeli, vom #2
Re: I need help on Array object, (summing number elements)
> i.e :
// extend the prototype of the array object:>
> myArray = newArray()
> myArray = [2,3,10];
> sum = Number(myArray[0]+myArray[1]+myArray[2]);
> trace (sum)
> //traces 15
>
> Anybody knows how to add number elementes of an array together
> dynamically?
>
> Instead of typing myArray[0] + myArray[0] etc. Řivind
Array.prototype.sum = function() {
var floatMin = parseFloat(this[0]);
// loop through all elements
for (var i = 1; i<this.length; i++) {
// check if element is a number
if (!isNaN(parseFloat(this[i]))) {
// sum up all numerical elements
floatMin += parseFloat(this[i]);
}
}
return floatMin;
};
// just a check if it works
var a = [12, 3, 5, "a"];
trace(a.sum());
Best wishes, Martin ;) * [url]http://birdy1976.com/[/url] * ICQ# 237743398
Martin Voegeli, vom Guest
-
Martin Voegeli, vom #3
Re: I need help on Array object, (summing number elements)
> // extend the prototype of the array object:
Sorry, I found a mistake in my code... This one is better:> Array.prototype.sum = function() {
> var floatMin = parseFloat(this[0]);
> // loop through all elements
> for (var i = 1; i<this.length; i++) {
> // check if element is a number
> if (!isNaN(parseFloat(this[i]))) {
> // sum up all numerical elements
> floatMin += parseFloat(this[i]);
> }
> }
> return floatMin;
> };
> // just a check if it works
> var a = [12, 3, 5, "a"];
> trace(a.sum());
// extend the prototype of the array object:
Array.prototype.sum = function() {
var floatMin = 0;
// loop through all elements
for (var i = 0; i<this.length; i++) {
// check if element is a number
if (!isNaN(parseFloat(this[i]))) {
// sum up all numerical elements
floatMin += parseFloat(this[i]);
}
}
return floatMin;
};
var a = ["foo", 12, 3, 5, "bar", 10, "foobar"];
trace(a.sum());
Best wishes, Martin ;) * [url]http://birdy1976.com/[/url] * ICQ# 237743398
Martin Voegeli, vom Guest
-



Reply With Quote

