Factoring out Code

Discussion in 'JavaScript' started by jwalker80, Jun 15, 2006.

  1. #1
    Hi,

    I have a function that is repeating itself and i reckon i can factor out a lot of the code. Check it out:

    function caldis(){
    
    	contotal = totalconns()
    
    	cons1 = parseInt(document.priceform.num1.value)
    	cons2 = parseInt(document.priceform.num2.value)
    	cons3 = parseInt(document.priceform.num3.value)
    	cons4 = parseInt(document.priceform.num4.value)
    	cons5 = parseInt(document.priceform.num5.value)
    	cons6 = parseInt(document.priceform.num6.value)
    	cons7 = parseInt(document.priceform.num7.value)
    	cons8 = parseInt(document.priceform.num8.value)
    	/*cons9 = parseInt(document.priceform.num9.value)
    	cons10 = parseInt(document.priceform.num10.value)
    	cons11 = parseInt(document.priceform.num11.value)
    	cons12 = parseInt(document.priceform.num12.value)
    	cons13 = parseInt(document.priceform.num13.value)*/
    	
    	pr1 = document.getElementById('price1').innerHTML.substring(1,10)
    	pr2 = document.getElementById('price2').innerHTML.substring(1,10)
    	pr3 = document.getElementById('price3').innerHTML.substring(1,10)
    	pr4 = document.getElementById('price4').innerHTML.substring(1,10)
    	pr5 = document.getElementById('price5').innerHTML.substring(1,10)
    	pr6 = document.getElementById('price6').innerHTML.substring(1,10)
    	pr7 = document.getElementById('price7').innerHTML.substring(1,10)
    	pr8 = document.getElementById('price8').innerHTML.substring(1,10)
    	
    	if (cons1 == 0){
    		document.getElementById('discount1new').innerHTML='\u0024'+0
    	}
    	else if (cons1 > 0){
    		
    
    document.getElementById('discount1new').innerHTML='\u0024'+discountPrice(pr1,contotal)
    	}
    	if (cons2 == 0){
    		document.getElementById('discount2new').innerHTML='\u0024'+0
    	}
    	else if (cons2 > 0){
    		
    
    document.getElementById('discount2new').innerHTML='\u0024'+discountPrice(pr2,contotal)
    	}
    	if (cons3 == 0){
    		document.getElementById('discount3new').innerHTML='\u0024'+0
    	}
    	else if (cons3 > 0){
    		
    
    document.getElementById('discount3new').innerHTML='\u0024'+discountPrice(pr3,contotal)
    	}
    	if (cons4 == 0){
    		document.getElementById('discount4new').innerHTML='\u0024'+0
    	}
    	else if (cons4 > 0){
    		
    
    document.getElementById('discount4new').innerHTML='\u0024'+discountPrice(pr4,contotal)
    	}
    	if (cons5 == 0){
    		document.getElementById('discount5new').innerHTML='\u0024'+0
    	}
    	else if (cons5 > 0){
    		
    
    document.getElementById('discount5new').innerHTML='\u0024'+discountPrice(pr5,contotal)
    	}
    	if (cons6 == 0){
    		document.getElementById('discount6new').innerHTML='\u0024'+0
    	}
    	else if (cons6 > 0){
    		
    
    document.getElementById('discount6new').innerHTML='\u0024'+discountPrice(pr6,contotal)
    	}
    	if (cons7 == 0){
    		document.getElementById('discount7new').innerHTML='\u0024'+0
    	}
    	else if (cons7 > 0){
    		
    
    document.getElementById('discount7new').innerHTML='\u0024'+discountPrice(pr7,contotal)
    	}
    	if (cons8 == 0){
    		document.getElementById('discount8new').innerHTML='\u0024'+0
    	}
    	else if (cons8 > 0){
    		
    
    document.getElementById('discount8new').innerHTML='\u0024'+discountPrice(pr8,contotal)
    	}
    	
    }
    Code (markup):
    Im thinking that I will pass a parameter through the function like

    function caldis(x){
    }
    Code (markup):
    then maybe put

    id = x to carry the number

    how would i refer to that throughout the function for example i want to do something like this:

    cons(id) = parseInt(document.priceform.num(id).value)
    Code (markup):
    instead of this:

    cons1 = parseInt(document.priceform.num1.value)
    cons2 = parseInt(document.priceform.num2.value)
    etc....
    Code (markup):
    I cant get the syntax right for this, or I am on the wrong path??
     
    jwalker80, Jun 15, 2006 IP