title: javascript total sum of array values
body: <p>Trying to create form to add up values from units array. Can't get function Calc_totals to work. </p>
  
  <pre><code>&lt;script language="JavaScript" type="text/javascript"&gt;
  function Calc(form) {
  
      var drink = new Array()
      drink [0] = form.drink0.value;
      drink [1] = form.drink1.value;
      drink [2] = form.drink2.value;
  
      var units = new Array()
      units [0] = 25;
      units [1] = 50;
      units [2] = 75;
  
      var number_units = new Array()
      form.units0.value = form.drink0.value * units[0];
      form.units1.value = form.drink1.value * units[1];
      form.units2.value = form.drink2.value * units[2]; 
      } 
  
  function Calc_totals() {
      var answerValue = 0; 
      for(i=0; i &lt; units.length; i++) 
      { 
      answerValue += Number(units[i]);
      } 
      form.total_units.value = answerValue;
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>I want the function Calc_totals to the total of units and output to form.total_units.value.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;form name="calc_form" id="form" method="post"&gt;
     &lt;table width="370" border="0" bgcolor="#EAEAEA"&gt;
  &lt;tr&gt;
  &lt;th width="141"&gt;&lt;h2&gt;Drink Type&lt;/h2&gt;&lt;/th&gt;&lt;th width="55"&gt;&lt;h2&gt;Number drank&lt;/h2&gt;&lt;/th&gt;
  &lt;th width="84"&gt;&lt;h2&gt;Units&lt;/h2&gt;&lt;/th&gt;
  &lt;th width="84"&gt;&lt;h2&gt;Calories&lt;/h2&gt;&lt;/th&gt;
  &lt;/tr&gt;
     &lt;tr class="table-text"&gt;
       &lt;td class="right"&gt;&lt;p class="table-text"&gt;Cider (4.5%)&lt;/p&gt;
         &lt;p class="table-text"&gt;1 x 568ml pint&lt;/p&gt;
         &lt;p class="table-text"&gt;e.g. Magners, Strongbow&lt;/p&gt;&lt;/td&gt;
      &lt;td width="55" valign="top"&gt;
        &lt;p class="table-text"&gt;
       &lt;input name="drink0" type="text" id="drink0" size="3" maxlength="2" onchange="Calc(form);" /&gt;
        &lt;/p&gt;&lt;/td&gt;
      &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
        &lt;input name="units0" type="text" id="units0" size="4" maxlength="3" /&gt;
      &lt;/p&gt;&lt;/td&gt;
      &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
        &lt;input name="calories0" type="text" id="calories0" size="4" maxlength="3" /&gt;
      &lt;/p&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td class="right"&gt;&lt;p class="table-text"&gt;Cider (7.5%)&lt;/p&gt;
        &lt;p class="table-text"&gt;1 x 500ml can&lt;/p&gt;&lt;/td&gt;
      &lt;td width="55" valign="top" class="table-text"&gt;&lt;p class="table-text"&gt;
        &lt;input name="drink1" type="text" id="drink1" size="3" maxlength="2" onchange="Calc(form);" /&gt;
      &lt;/p&gt;&lt;/td&gt;
      &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
        &lt;input name="units1" type="text" id="units1" size="4" maxlength="3" /&gt;
        &lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
      &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
        &lt;input name="calories1" type="text" id="calories1" size="4" maxlength="3" /&gt;
        &lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td class="right"&gt;&lt;p class="table-text"&gt;Beer (5%)&lt;/p&gt;
        &lt;p class="table-text"&gt;1 x 330ml bottle &lt;/p&gt;
        &lt;p class="table-text"&gt;e.g. Grolsch, Budweiser&lt;/p&gt;&lt;/td&gt;
      &lt;td width="55" valign="top" class="table-text"&gt;&lt;p class="table-text"&gt;
        &lt;input name="drink2" type="text" id="drink2" size="3" maxlength="2" onchange="Calc(form);" /&gt;
      &lt;/p&gt;&lt;/td&gt;
      &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
        &lt;input name="units2" type="text" id="units2" size="4" maxlength="3" /&gt;
      &lt;/p&gt;&lt;/td&gt;
      &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
        &lt;input name="calories2" type="text" id="calories2" size="4" maxlength="3" /&gt;
      &lt;/p&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
  &lt;tr&gt;
    &lt;td&gt;&lt;p class="table-text"&gt;&amp;nbsp;&lt;/p&gt;&lt;/td&gt;
    &lt;td width="55" valign="top"&gt;&lt;p class="table-text"&gt;&lt;/p&gt;&lt;/td&gt;
    &lt;td width="84" valign="top"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;&lt;/p&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
    &lt;td&gt;&lt;p class="table-text"&gt;&lt;strong&gt;Totals per week= &lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
    &lt;td width="55" valign="top"&gt;&lt;p class="table-text"&gt;&amp;nbsp;&lt;/p&gt;&lt;/td&gt;
    &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
      &lt;input name="total_units" type="text" id="total_units" size="4" maxlength="3" /&gt; 
      units  &lt;/p&gt;&lt;/td&gt;
    &lt;td width="84" valign="top"&gt;&lt;p class="table-text"&gt;
      &lt;input name="total_calories" type="text" id="total" size="4" maxlength="3" /&gt;
    kcals&lt;/p&gt;&lt;/td&gt;
  &lt;/tr&gt; 
  
  &lt;tr&gt;  
  &lt;td colspan="2"&gt;&lt;INPUT name="reset" value="Reset" TYPE="reset"&gt; &lt;/td&gt;
  &lt;td colspan="3"&gt;&lt;input name="Calculate Total" type="button" id="Calculate Total" value="Calculate Total" onclick="Calc_totals();" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I want the total of units.
  Thanks for any help.</p>