JavaScript loop?

Discussion in 'JavaScript' started by zk0, Feb 23, 2007.

  1. #1
    This is probably very easy to do but Im new to javascript so please do not laugh. :)


    I want to do some kind of loop that takes this array (look below) and basically do 10 + 5 + 2 + 6 + 2 + 9 + 6 + 7 + 8 + 9 =

    var myArr = new Array( 10, 5, 2, 6, 2, 9, 6, 7, 8, 9 );
    Code (markup):
    Thanks!
     
    zk0, Feb 23, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You want to sum all values? Try this:

    
    
    var total = 0;
    
    for (var i = 0; i < myArr.length; i++)
    {
         total += myArr[i];
    }
    
    document.write(total);
    
    
    Code (javascript):
     
    nico_swd, Feb 23, 2007 IP
    zk0 likes this.
  3. zk0

    zk0 Peon

    Messages:
    299
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you "nico_swd"! :)
     
    zk0, Feb 24, 2007 IP