I need help on Array object, (summing number elements)

Ask a Question related to Macromedia Flash, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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: ...
    5. 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...
  3. #2

    Default Re: 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
    // extend the prototype of the array object:
    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

  4. #3

    Default Re: I need help on Array object, (summing number elements)

    > // extend the prototype of the array object:
    > 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());
    Sorry, I found a mistake in my code... This one is better:

    // 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

  5. #4

    Default Re: I need help on Array object, (summing number elements)

    THANX MAN!

    Řivind


    oiv Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139