Web Analytics Made Easy -
StatCounter Weekly Compounding calculator with weekly deposits - CodingForum

Announcement

Collapse
No announcement yet.

Weekly Compounding calculator with weekly deposits

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Weekly Compounding calculator with weekly deposits

    Hi there,
    I am trying to set a calculator up which calculates the cost of a vehicle over a year, 5 years and 10 years compounding weekly at an inflation rate set by user.

    I put together the following code with my limited knowledge of javascript and can't seem to get it to work. I have got stuck and would really appreciate some help.

    Thank you in advance


    Code:
      <html>  
          
        <head>  
         
        <script language="javascript" type="text/javascript">  
        function calculate(){  
        a=Number(document.calculator.petrol.value);  
        b=Number(document.calculator.tuneups.value);
    	c=Number(document.calculator.oilchanges.value);
    	d=Number(document.calculator.cambelt.value);
    	e=Number(document.calculator.exhaust.value);
    	f=Number(document.calculator.coolingsystem.value);
    	g=Number(document.calculator.inflation.value);
        
    	h=Number(document.calculator.period1.value);
    	i=Number(document.calculator.period2.value);
    	j=Number(document.calculator.period3.value);
    	k=Number(document.calculator.period4.value);
    	l=Number(document.calculator.period5.value);
    	m=Number(document.calculator.period6.value);
    	
    	w1=1;
    	w2=w1*g/52+w1; 
    	w3=w2*g/52+w2;
    	w4=w3*g/52+w3;
    	w5=w4*g/52+w4;
    	w6=w5*g/52+w5;
    	w7=w6*g/52+w6;
    	w8=w7*g/52+w7;
    	w9=w8*g/52+w8;
    	w10=w9*g/52+w9;	
    	w11=w10*g/52+w10;
    
    	w12=w11*g/52+w11; 
    	w13=w12*g/52+w12;
    	w14=w13*g/52+w13;
    	w15=w14*g/52+w14;
    	w16=w15*g/52+w15;
    	w17=w16*g/52+w16;
    	w18=w17*g/52+w17;
    	w19=w18*g/52+w18;
    	w20=w19*g/52+w19;	
    	w21=w20*g/52+w20;
    
    	w22=w21*g/52+w21; 
    	w23=W22*g/52+w22;
    	w24=w23*g/52+w23;
    	w25=w24*g/52+w24;
    	w26=w25*g/52+w25;
    	w27=w26*g/52+w26;
    	w28=w27*g/52+w27;
    	w29=w28*g/52+w28;
    	w30=w29*g/52+w29;	
    	w31=w30*g/52+w30;
    
    	w32=w31*g/52+w31; 
    	w33=w32*g/52+w32;
    	w34=w33*g/52+w33;
    	w35=w34*g/52+w34;
    	w36=w35*g/52+w35;
    	w37=w36*g/52+w36;
    	w38=w37*g/52+w37;
    	w39=w38*g/52+w38;
    	w40=w39*g/52+w39;	
    	w41=w40*g/52+w40;
    
    	w42=w41*g/52+w41; 
    	w43=w42*g/52+w42;
    	w44=w43*g/52+w43;
    	w45=w44*g/52+w44;
    	w46=w45*g/52+w45;
    	w47=w46*g/52+w46;
    	w48=w47*g/52+w47;
    	w49=w48*g/52+w48;
    	w50=w49*g/52+w49;	
    	w51=w50*g/52+w50;
    	w52=w51*g/52+w51;
    
    
    swy=w1+w2+w3+w4+w5+w6+w7+w8+w9+w10+w11+w12+w13+w14+w15+w16+w17+w18+w19+w20+w21+w22+w23+w24+w25+w26+w27+w28+w29+w30+w31+w32+w33+w34+w35+w36+w37+w38+w39+w40+w41+w42+w43+w44+w45+w46+w47+w48+w49+w50+w51+w52;
    	
    o=a*h*swy;
    	p=b*i*swy;
    	q=c*j*swy;
    	r=d*k*swy;
    	s=e*l*swy;
    	t=f*m*swy;
    
        document.calculator.ytotal1.value=o.toFixed(2);
    	document.calculator.ytotal2.value=p.toFixed(2); 
    	document.calculator.ytotal3.value=q.toFixed(2); 
    	document.calculator.ytotal4.value=r.toFixed(2); 
    	document.calculator.ytotal5.value=s.toFixed(2);
    	document.calculator.ytotal6.value=t.toFixed(2);
    	document.calculator.ytotal.value=n.toFixed(2);
        }
    	
    	 function clearMe(formfield){
      if (formfield.defaultValue==formfield.value)
       formfield.value = ""
     }
    
    function returnMe(formfield){
     if (formfield.value=="")
      formfield.value = formfield.defaultValue} 
    
        </script> 
     
        </head>  
          
        <body>  
          
        
        <form name="calculator">  
        <table cellpadding="10">
    <tr>
    <td>Costs</td>
    <td></td>
    <td>Period</td>
    <td> 1st Year Cost </td>
    </tr>
    
    <tr>
    <td>Petrol</td>
    <td>$ <input type="text" name="petrol" size="8" onfocus="clearMe(this)" onblur="returnMe(this)" ></td>
    <td>       <select name="period1">
            <option value="1">Weekly</option>
            <option value="0.2308">Monthly</option>
            <option value="0.01923">Yearly</option>
            <option value="0.009615">2 Yearly</option>
    		<option value="0.006410">3 Yearly</option>
    		<option value="0.004808">4 Yearly</option>
    		<option value="0.003846">5 Yearly</option>
          </select></td>
    <td>$ <input type="text" name="ytotal1"  size="8"></td>
     
    <tr>
    <td>Tune ups</td>
    <td>$ <input type="text" name="tuneups" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
    <td>       <select name="period1">
            <option value="1">Weekly</option>
            <option value="0.2308">Monthly</option>
            <option value="0.01923">Yearly</option>
            <option value="0.009615">2 Yearly</option>
    		<option value="0.006410">3 Yearly</option>
    		<option value="0.004808">4 Yearly</option>
    		<option value="0.003846">5 Yearly</option>
          </select></td>
    <td>$ <input type="text" name="ytotal2" size="8"></td>
    </tr>
    
    <tr>
    <td>Oil Changes</td>
    <td>$ <input type="text" name="oilchanges" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
    <td>       <select name="period1">
            <option value="1">Weekly</option>
            <option value="0.2308">Monthly</option>
            <option value="0.01923">Yearly</option>
            <option value="0.009615">2 Yearly</option>
    		<option value="0.006410">3 Yearly</option>
    		<option value="0.004808">4 Yearly</option>
    		<option value="0.003846">5 Yearly</option>
          </select></td>
    <td>$ <input type="text" name="ytotal3" size="8"></td>
    </tr>
    <tr>
    <td>Cam Belt</td>
    <td>$ <input type="text" name="cambelt"  size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
    <td>       <select name="period1">
            <option value="1">Weekly</option>
            <option value="0.2308">Monthly</option>
            <option value="0.01923">Yearly</option>
            <option value="0.009615">2 Yearly</option>
    		<option value="0.006410">3 Yearly</option>
    		<option value="0.004808">4 Yearly</option>
    		<option value="0.003846">5 Yearly</option>
          </select></td>
    <td>$ <input type="text" name="ytotal4"  size="8"></td>
    </tr>
    <tr>
    <td>Exhaust System</td>
    <td>$ <input type="text" name="exhaust" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
    <td>       <select name="period1">
            <option value="1">Weekly</option>
            <option value="0.2308">Monthly</option>
            <option value="0.01923">Yearly</option>
            <option value="0.009615">2 Yearly</option>
    		<option value="0.006410">3 Yearly</option>
    		<option value="0.004808">4 Yearly</option>
    		<option value="0.003846">5 Yearly</option>
          </select></td>
    <td>$ <input type="text" name="ytotal5"  size="8"></td>
    </tr>
    <tr>
    <td>Cooling System</td>
    <td>$ <input type="text" name="coolingsystem" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
    <td>       <select name="period1">
            <option value="1">Weekly</option>
            <option value="0.2308">Monthly</option>
            <option value="0.01923">Yearly</option>
            <option value="0.009615">2 Yearly</option>
    		<option value="0.006410">3 Yearly</option>
    		<option value="0.004808">4 Yearly</option>
    		<option value="0.003846">5 Yearly</option>
          </select></td>
    <td>$ <input type="text" name="ytotal6" size="8"></td>
    </tr>
    
    <tr>
    <td>Inflation %</td>
    <td>%<input type="text" name="inflation" value = "5"  size="8"  onfocus="clearMe(this)" onblur="returnMe(this)"></td>
     </tr>
    <tr><td></td>
    <td></td>
    <td><strong>Total</strong></td>
    <td>$ <input type="text" name="ytotal" size="8"></td>
    </table> 
       
        <input type="button" value="Calculate" onclick="javascript:calculate();">  
    
          
        </form>  
          
        </body>  
        </html>

  • #2
    For starters your selects
    <select name="period1">

    all have period1. They should go up to period6

    Your w 1~52 stops calculating at w22. Possibly need to recast data type.
    Last edited by sunfighter; Aug 24, 2011, 10:55 AM.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      Thank you for your quick reply. I have corrected and now have it working.

      See below.
      Code:
          <html>  
            
          <head>  
           
          <script language="javascript" type="text/javascript">  
          function calculate(){  
          a=Number(document.calculator.petrol.value);  
          b=Number(document.calculator.tuneups.value);
      	c=Number(document.calculator.oilchanges.value);
      	d=Number(document.calculator.cambelt.value);
      	e=Number(document.calculator.exhaust.value);
      	f=Number(document.calculator.coolingsystem.value);
      	n=Number(document.calculator.inflation.value);
          g=n/100;
      
      	h=Number(document.calculator.period1.value);
      	i=Number(document.calculator.period2.value);
      	j=Number(document.calculator.period3.value);
      	k=Number(document.calculator.period4.value);
      	l=Number(document.calculator.period5.value);
      	m=Number(document.calculator.period6.value);
      	
      	w1=1;
      	w2=(w1*g/52)+w1; 
      	w3=(w2*g/52)+w2;
      	w4=(w3*g/52)+w3;
      	w5=(w4*g/52)+w4;
      	w6=(w5*g/52)+w5;
      	w7=(w6*g/52)+w6;
      	w8=(w7*g/52)+w7;
      	w9=(w8*g/52)+w8;
      	w10=(w9*g/52)+w9;	
      	w11=(w10*g/52)+w10;
      
      	w12=(w11*g/52)+w11; 
      	w13=(w12*g/52)+w12;
      	w14=(w13*g/52)+w13;
      	w15=(w14*g/52)+w14;
      	w16=(w15*g/52)+w15;
      	w17=(w16*g/52)+w16;
      	w18=(w17*g/52)+w17;
      	w19=(w18*g/52)+w18;
      	w20=(w19*g/52)+w19;	
      	w21=(w20*g/52)+w20;
      
      	w22=(w21*g/52)+w21; 
      	w23=(w22*g/52)+w22;
      	w24=(w23*g/52)+w23;
      	w25=(w24*g/52)+w24;
      	w26=(w25*g/52)+w25;
      	w27=(w26*g/52)+w26;
      	w28=(w27*g/52)+w27;
      	w29=(w28*g/52)+w28;
      	w30=(w29*g/52)+w29;	
      	w31=(w30*g/52)+w30;
      
      	w32=(w31*g/52)+w31; 
      	w33=(w32*g/52)+w32;
      	w34=(w33*g/52)+w33;
      	w35=(w34*g/52)+w34;
      	w36=(w35*g/52)+w35;
      	w37=(w36*g/52)+w36;
      	w38=(w37*g/52)+w37;
      	w39=(w38*g/52)+w38;
      	w40=(w39*g/52)+w39;	
      	w41=(w40*g/52)+w40;
      
      	w42=(w41*g/52)+w41; 
      	w43=(w42*g/52)+w42;
      	w44=(w43*g/52)+w43;
      	w45=(w44*g/52)+w44;
      	w46=(w45*g/52)+w45;
      	w47=(w46*g/52)+w46;
      	w48=(w47*g/52)+w47;
      	w49=(w48*g/52)+w48;
      	w50=(w49*g/52)+w49;	
      	w51=(w50*g/52)+w50;
      	w52=(w51*g/52)+w51;
      
      
      swy=w1+w2+w3+w4+w5+w6+w7+w8+w9+w10+w11+w12+w13+w14+w15+w16+w17+w18+w19+w20+w21+w22+w23+w24+w25+w26+w27+w28+w29+w30+w31+w32+w33+w34+w35+w36+w37+w38+w39+w40+w41+w42+w43+w44+w45+w46+w47+w48+w49+w50+w51+w52;
      	
      o=a*h*swy;
      	p=b*i*swy;
      	q=c*j*swy;
      	r=d*k*swy;
      	s=e*l*swy;
      	t=f*m*swy;
      
          document.calculator.ytotal1.value=o.toFixed(2);
      	document.calculator.ytotal2.value=p.toFixed(2); 
      	document.calculator.ytotal3.value=q.toFixed(2); 
      	document.calculator.ytotal4.value=r.toFixed(2); 
      	document.calculator.ytotal5.value=s.toFixed(2);
      	document.calculator.ytotal6.value=t.toFixed(2);
          }
      	
      	 function clearMe(formfield){
        if (formfield.defaultValue==formfield.value)
         formfield.value = ""
       }
      
      function returnMe(formfield){
       if (formfield.value=="")
        formfield.value = formfield.defaultValue} 
      
          </script> 
       
          </head>  
            
          <body>  
            
          
          <form name="calculator">  
          <table cellpadding="10">
      <tr>
      <td>Costs</td>
      <td></td>
      <td>Period</td>
      <td> 1st Year Cost </td>
      </tr>
      
      <tr>
      <td>Petrol</td>
      <td>$ <input type="text" name="petrol" size="8" onfocus="clearMe(this)" onblur="returnMe(this)" ></td>
      <td>       <select name="period1">
              <option value="1">Weekly</option>
              <option value="0.2308">Monthly</option>
              <option value="0.01923">Yearly</option>
              <option value="0.009615">2 Yearly</option>
      		<option value="0.006410">3 Yearly</option>
      		<option value="0.004808">4 Yearly</option>
      		<option value="0.003846">5 Yearly</option>
            </select></td>
      <td>$ <input type="text" name="ytotal1"  size="8"></td>
       
      <tr>
      <td>Tune ups</td>
      <td>$ <input type="text" name="tuneups" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
      <td>       <select name="period2">
              <option value="1">Weekly</option>
              <option value="0.2308">Monthly</option>
              <option value="0.01923">Yearly</option>
              <option value="0.009615">2 Yearly</option>
      		<option value="0.006410">3 Yearly</option>
      		<option value="0.004808">4 Yearly</option>
      		<option value="0.003846">5 Yearly</option>
            </select></td>
      <td>$ <input type="text" name="ytotal2" size="8"></td>
      </tr>
      
      <tr>
      <td>Oil Changes</td>
      <td>$ <input type="text" name="oilchanges" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
      <td>       <select name="period3">
              <option value="1">Weekly</option>
              <option value="0.2308">Monthly</option>
              <option value="0.01923">Yearly</option>
              <option value="0.009615">2 Yearly</option>
      		<option value="0.006410">3 Yearly</option>
      		<option value="0.004808">4 Yearly</option>
      		<option value="0.003846">5 Yearly</option>
            </select></td>
      <td>$ <input type="text" name="ytotal3" size="8"></td>
      </tr>
      <tr>
      <td>Cam Belt</td>
      <td>$ <input type="text" name="cambelt"  size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
      <td>       <select name="period4">
              <option value="1">Weekly</option>
              <option value="0.2308">Monthly</option>
              <option value="0.01923">Yearly</option>
              <option value="0.009615">2 Yearly</option>
      		<option value="0.006410">3 Yearly</option>
      		<option value="0.004808">4 Yearly</option>
      		<option value="0.003846">5 Yearly</option>
            </select></td>
      <td>$ <input type="text" name="ytotal4"  size="8"></td>
      </tr>
      <tr>
      <td>Exhaust System</td>
      <td>$ <input type="text" name="exhaust" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
      <td>       <select name="period5">
              <option value="1">Weekly</option>
              <option value="0.2308">Monthly</option>
              <option value="0.01923">Yearly</option>
              <option value="0.009615">2 Yearly</option>
      		<option value="0.006410">3 Yearly</option>
      		<option value="0.004808">4 Yearly</option>
      		<option value="0.003846">5 Yearly</option>
            </select></td>
      <td>$ <input type="text" name="ytotal5"  size="8"></td>
      </tr>
      <tr>
      <td>Cooling System</td>
      <td>$ <input type="text" name="coolingsystem" size="8" onfocus="clearMe(this)" onblur="returnMe(this)"></td>
      <td>       <select name="period6">
              <option value="1">Weekly</option>
              <option value="0.2308">Monthly</option>
              <option value="0.01923">Yearly</option>
              <option value="0.009615">2 Yearly</option>
      		<option value="0.006410">3 Yearly</option>
      		<option value="0.004808">4 Yearly</option>
      		<option value="0.003846">5 Yearly</option>
            </select></td>
      <td>$ <input type="text" name="w2" size="8"></td>
      </tr>
      
      <tr>
      <td>Inflation %</td>
      <td>%<input type="text" name="inflation" value = "5"  size="8"  onfocus="clearMe(this)" onblur="returnMe(this)"></td>
       </tr>
      <tr><td></td>
      <td></td>
      <td><strong>Total</strong></td>
      <td>$ <input type="text" name="ytotal" size="8"></td>
      </table> 
         
          <input type="button" value="Calculate" onclick="javascript:calculate();">  
      
            
          </form>  
            
          </body>  
          </html>

      Comment


      • #4
        Thank you for coming back and sharing what you did. I checked out the second script and find that 1st year costs do not calc or show up in Cooling System and TOTAL.

        If you do read this and get things working please mark the thread solved.
        Evolution - The non-random survival of random variants.
        Physics is actually atoms trying to understand themselves.

        Comment

        Working...
        X