# Thread: arrays - how to improve code

1. Member
Join Date
Oct 2012
Location
india
Posts
12

## arrays - how to improve code

I have written the followinf code using arrays and based on this my calculator at http://airbiopf.org is working in order. But what i want to know that I am using arrays at present for 2 quarters each for July-Sept and Oct-Dec.I now need 2 more quarterly averages for Jan-Mar and Apr-june quarters.So please let me know a more efficient and proper way for the function SumAverage() either through arrays /loops or otherwise so that I need not create array for each quarter separately and avoid repeating the same calculation for Average for each quarter with differnt named variables.

I hope, as a learner , there must be a better and more efficient and concise code with arrays/loops or otherwise on the basis of which I may be able to avoid frequent repetition of
thanks
vkwd7
Code:
``` function SumAverage()
{
var indxnos = new Array(3);
indxnos[0]= document.DA_Slabs.jul12.value;
indxnos[1]= document.DA_Slabs.aug12.value;
indxnos[2]= document.DA_Slabs.sep12.value;
var indxnoss=new Array(3)
indxnoss[0]= document.DA_Slabs.oct12.value;
indxnoss[1]= document.DA_Slabs.nov12.value;
indxnoss[2]= document.DA_Slabs.dec12.value;
var Sum = 0;
var NmbVle = 0;
var Vle;
var Average;
for (var i = 0; i < indxnos.length; i++) if (Vle=parseInt(indxnos[i]))
{
Sum=Sum+Vle;
NmbVle++;}
Average = Sum/NmbVle;
document.DA_Slabs.avg.value= Average.toFixed(2);

// repetition starts here for the next quarter
var Sums = 0;
var NmbVles = 0;
var Vles;
var Average2;
for (var i = 0; i < indxnoss.length; i++) if (Vles=parseInt(indxnoss[i]))

{
Sums=Sums+Vles;
NmbVles++;}

Average2=Sums/NmbVles
document.DA_Slabs.avg2.value= Average2.toFixed(2);
var slabs= ((((((Average*4.63*4.93)))-2836)/4)-468);

document.DA_Slabs.slabs.value=Math.floor(slabs)
var cat1=Math.round (slabs)*0.15
var cat2=Math.round (slabs)*0.18
var cat3= Math.round(slabs)*0.24
var cat4= Math.round(slabs)*0.35
var cat5= Math.round(slabs)*0.67

document.DA_Slabs.cat_1.value=cat1.toFixed(2)+("%")
document.DA_Slabs.cat_2.value=cat2.toFixed(2) +("%")
document.DA_Slabs.cat_3.value=cat3.toFixed(2) +("%")
document.DA_Slabs.cat_4.value=cat4.toFixed(2)+("%")
document.DA_Slabs.cat_5.value=cat5.toFixed(2) +("%")
if (isNaN(Average2))
{ alert(" enter a index number of your choice in cell under Oct 12-Dec 12 and then click calculate")
}
var slabs2= ((((((Average2*4.63*4.93)))-2836)/4)-468);

document.DA_Slabs.slabs2.value=Math.floor(slabs2)
var cat11=Math.round (slabs2)*0.15
var cat12=Math.round (slabs2)*0.18
var cat13= Math.round(slabs2)*0.24
var cat14= Math.round(slabs2)*0.35
var cat15= Math.round(slabs2)*0.67

document.DA_Slabs.cat_11.value=cat11.toFixed(2)+("%")
document.DA_Slabs.cat_12.value=cat12.toFixed(2) +("%")
document.DA_Slabs.cat_13.value=cat13.toFixed(2) +("%")
document.DA_Slabs.cat_14.value=cat14.toFixed(2)+("%")
document.DA_Slabs.cat_15.value=cat15.toFixed(2) +("%")

}

</script>```

2. Registered User
Join Date
Mar 2011
Posts
128
If you're repeating alot of code with different variable, you should try to make functions out of it.

Code:
```function SumAverage(){

function calculate_quarter(months, categories, avgNode, slabsNode){
var multipliers = [0.15, 0.18, 0.24, 0.35, 0.67],
Sum = 0,
NmbVle = 0,
indxnos,
Vle,
Average,
slabs,
i;

for(i=0; i<months.length; i++){
indxnos = document.DA_Slabs[months[i]].value;
if(Vle = parseInt(indxnos)){
Sum = Sum + Vle;
NmbVle++;
}
}

Average = Sum / NmbVle;
document.DA_Slabs[avgNode].value = Average.toFixed(2);

slabs= (((Average * 22,8259) - 2836) / 4) - 468;
document.DA_Slabs[slabsNode].value = Math.floor(slabs);

for(i=0; i<categories.length; i++){
document.DA_Slabs[categories[i]].value = (Math.round(slabs) * multipliers[i]).toFixed(2) + "%";
}
};//end func

calculate_quarter(['jul12', 'aug12', 'sep12'], ['cat_1', 'cat_2', 'cat_3', 'cat_4', 'cat_5'], 'avg', 'slabs');
calculate_quarter(['oct12', 'nov12', 'dec12'], ['cat_11', 'cat_12', 'cat_13', 'cat_14', 'cat_15'], 'avg2', 'slabs2');

};//end func```

3. Member
Join Date
Oct 2012
Location
india
Posts
12
thanks
being a learner what I followed is that I have to create arrays for months[0-11],catergories[0-11], and avgNode[0-3],slabsnode[0-4] and accordingly create relevant input boxes for inserting values in them . Is this the way to proceed or am I missing what I need to follow.
regards
vkwd7

4. Registered User
Join Date
Mar 2011
Posts
128
Yes, add the input boxes and call the function calculate_quarter, for the four quarters you need.
1st argument is an array with the names of the month input boxes for that quarter.
2nd argument is an array with the names of category input boxes for that quarter.
3rd is the name of the average input box for that quarter.
4th is the name of slabs input box for that quarter.

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.13848 seconds
• Memory Usage 2,871KB
• Queries Executed 15 (?)
Template Usage (33):
• (2)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (4)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (4)postbit
• (4)postbit_onlinestatus
• (4)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 (72):
• 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_complete
• forumrules
• navbits
• navbits_complete