www.webdeveloper.com
Results 1 to 4 of 4

Thread: arrays - how to improve code

  1. #1
    Join Date
    Oct 2012
    Posts
    6

    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>

  2. #2
    Join Date
    Mar 2011
    Posts
    60
    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. #3
    Join Date
    Oct 2012
    Posts
    6
    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.
    Please advise.
    regards
    vkwd7

  4. #4
    Join Date
    Mar 2011
    Posts
    60
    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.

Thread Information

Users Browsing this Thread

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
  •  
HTML5 Development Center



Recent Articles