# arrays - how to improve code

• 11-03-2012, 02:10 AM
vkwd7
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++;}                                 //alert(indxnos[i])                                 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>```
• 11-03-2012, 11:31 AM
Kever
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```
• 11-05-2012, 06:03 AM
vkwd7
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
• 11-05-2012, 12:02 PM
Kever
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.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.11731 seconds
• Memory Usage 2,378KB
• Queries Executed 11 (?)
Template Usage (20):
• (2)bbcode_code_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