www.webdeveloper.com
Results 1 to 8 of 8

Thread: Newb Array Question

  1. #1
    Join Date
    Aug 2008
    Posts
    3

    Newb Array Question

    Hi Everyone,

    I'm just learning JavaScript through a Lynda.com tutorial, and I'm trying to push myself beyond some of the examples in each chapter.

    Right now I'm on arrays. One of the examples they went through was adding all of the values in the array:

    Code:
    var myArray = [500, 500, 500, 500, 500];
    
    var total = 0;
    
    for ( var i = 0; i < myArray.length; i++ ) {
    	total = total + myArray[i];
    }
    
    document.write("The total is: " + total);
    Okay so I understand this. My challenge, which I can't seem to solve by myself, is this: What if I had an array with 6 values, and I wanted to add the first two numbers, then subtract the third? And loop through this each time.

    For example:

    Code:
    var myArray = [35, 250, 75, 60, 90, 100];
    My desired output would be:

    210 (result from 35 + 250 - 75)
    260 (result from 210 + 60 + 90 - 100)

    What are some ways I could accomplish this?

    Thank you!

    John

  2. #2
    Join Date
    Nov 2010
    Posts
    978
    hmmm... I suspect that this is not what you are looking for,but it may be of interest anyway. You can access array values directly by their position in the array:
    Code:
    <html>
    <title></title>
    </head>
    <body>
    
    <script language="javascript">
    var myArray = [35, 250, 75, 60, 90, 100];
    sum1=myArray[0]+myArray[1]-myArray[2];
    alert("sum 1= "+sum1)
    sum2=sum1+myArray[3]+myArray[4]-myArray[5]
    alert("sum 2= "+sum2)
    </script>
    </body>
    </html>

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    You can too change the sign after each addition

    Code:
    var myArray = [],i,sign=1;
    for (i=0;i<2000;i++) myArray[i] =1/(2*i+1);
    var alternatedTotal = 0,oldAlternatedTotal;
    for ( var i = 0; i < myArray.length; i++ ) {
    	oldAlternatedTotal=alternatedTotal;
    	alternatedTotal += sign*myArray[i];
    	sign =- sign;
    }
    document.write("<p>The alternatedTotal is : " + alternatedTotal+" and<br>"+(4*alternatedTotal)+"< Pi <"+(4*oldAlternatedTotal)+"</p>");
    And you notice that Pi/4 = 1-1/3+1/5-1/7+1/9....
    Last edited by 007Julien; 11-25-2011 at 06:48 PM.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,257

    Lightbulb

    Another alternative solution...
    Code:
    <html>
    <title></title>
    </head>
    <body>
    <script language="javascript">
    var myArray = [35, 250, 75, 60, 90, 100];
    var sum = 0;
    var total = 0;
    for (var i=0; i<myArray.length; i=i+3) {
      sum = myArray[i]+myArray[i+1]-myArray[i+2];
      total += sum;
      alert(sum+' : '+total);
    }
    alert('Total: '+total);
    </script>
    </body>
    </html>

  5. #5
    Join Date
    Aug 2008
    Posts
    3
    Thank you, everyone, for your time! This is extremely helpful.


    Kindly,
    John

  6. #6
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Another method might include...
    Code:
    var myArray = [35, 250, 75, 60, 90, 100];
    // add first two and then subtract the third & repeat...
    modVal = myArray.length / 2;
    for(c=0,total=0; c<myArray.length; c++)
    	 total += ( (c+1)&#37;modVal == 0 )? -(myArray[c]) : myArray[c];
    
    alert(total);// outputs 260
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  7. #7
    Join Date
    Sep 2007
    Posts
    301
    Code:
       
    <script type="text/javascript">
    
    var B = ["+","+","-","+","+","-"];
    var A = [35, 250, 75, 60, 90, 100];
    var str = "";
    for(var i=0; i< A.length; i++) {
    str += B[i] + A[i];
    }
    alert( "str = " + str ); // +35+250-75+60+90-100
    
    alert( "eval( str ) = " + eval( str ) ); // 260
    
    </script>
    http://www.w3schools.com/jsref/jsref_eval.asp
    Last edited by Ayşe; 11-26-2011 at 05:12 PM.
    İyiliği emret. Kötülükten alıkoy. (31/16)

    hasbunallahuVeNimelVekil++

  8. #8
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by Ayşe View Post
    Code:
       
    <script type="text/javascript">
    
    var B = ["+","+","-","+","+","-"];
    var A = [35, 250, 75, 60, 90, 100];
    var str = "";
    for(var i=0; i< A.length; i++) {
    str += B[i] + A[i];
    }
    alert( "str = " + str ); // +35+250-75+60+90-100
    
    alert( "eval( str ) = " + eval( str ) ); // 260
    
    </script>
    http://www.w3schools.com/jsref/jsref_eval.asp
    I am afraid that THIS is another example of an abuse of eval and why it gets such a bad name. eval should only be used as a last resort.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles