# Newb Array Question

• 11-25-2011, 05:04 PM
hollywoodjrc
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
• 11-25-2011, 05:46 PM
xelawho
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>```
• 11-25-2011, 07:26 PM
007Julien
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....
• 11-25-2011, 08:46 PM
JMRKER
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>```
• 11-26-2011, 01:38 AM
hollywoodjrc
Thank you, everyone, for your time! This is extremely helpful.

Kindly,
John
• 11-26-2011, 05:09 PM
JunkMale
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```
• 11-26-2011, 06:07 PM
Ayşe
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
• 11-26-2011, 11:08 PM
JunkMale
Quote:

Originally Posted by Ayşe
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.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.10607 seconds
• Memory Usage 2,372KB
• Queries Executed 11 (?)
Template Usage (21):
• (8)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates