1. Registered User
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. Registered User
Join Date
Nov 2010
Posts
1,276
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>
<body>

<script language="javascript">
var myArray = [35, 250, 75, 60, 90, 100];
sum1=myArray[0]+myArray[1]-myArray[2];
sum2=sum1+myArray[3]+myArray[4]-myArray[5]
</script>
</body>
</html>```

3. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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 07:48 PM.

4. Another alternative solution...
Code:
```<html>
<title></title>
<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;
}
</script>
</body>
</html>```

5. Registered User
Join Date
Aug 2008
Posts
3

Kindly,
John

6. 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];

7. Registered User
Join Date
Sep 2007
Posts
390
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 06:12 PM.

8. 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.

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

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09955 seconds
• Memory Usage 2,926KB
• Queries Executed 15 (?)
Template Usage (34):
• (8)bbcode_code
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (8)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (8)postbit
• (8)postbit_onlinestatus
• (8)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./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/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (73):
• init_startup
• friendlyurl_resolve_class
• 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_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete