Note that there are some explanatory texts on larger screens.

plurals
  1. POComplex Javascript Array - sum values and insert value into form
    primarykey
    data
    text
    <p>Trying to adapt a rather complex array code. Basically, I want to add the total hours entered, then return the value into input name="total_hrs", but got stuck! Would also appreciate any help to simplify/clean up the script! Thanks. Here's the script:</p> <pre><code>&lt;script language="JavaScript" type="text/javascript"&gt; &lt;!-- function subCalc(the_form) { var subtotal = 0; var subtemp = 0; var totalHrs = 0; // put the calories and form fields into parallel arrays. var calorie_array = new Array() calorie_array[0] = 72; calorie_array[1] = 86; calorie_array[2] = 100; calorie_array[3] = 126; calorie_array[4] = 130; calorie_array[5] = 180; calorie_array[6] = 156; calorie_array[7] = 182; calorie_array[8] = 210; calorie_array[9] = 250; calorie_array[10] = 270; calorie_array[11] = 390; calorie_array[12] = 390; calorie_array[13] = 546; var time_array = new Array() time_array[0] = the_form.sleeping_time; time_array[1] = the_form.TV_time; time_array[2] = the_form.sit_time; time_array[3] = the_form.cook_time; time_array[4] = the_form.stand_time; time_array[5] = the_form.wash_time; time_array[6] = the_form.walk_S_time; time_array[7] = the_form.house_time; time_array[8] = the_form.walk_M_time; time_array[9] = the_form.gard_time; time_array[10] = the_form.danc_time; time_array[11] = the_form.stairs_time; time_array[12] = the_form.jog_time; time_array[13] = the_form.sqsh_time; var sub_array = new Array() sub_array[0] = the_form.cal0; sub_array[1] = the_form.cal1; sub_array[2] = the_form.cal2; sub_array[3] = the_form.cal3; sub_array[4] = the_form.cal4; sub_array[5] = the_form.cal5; sub_array[6] = the_form.cal6; sub_array[7] = the_form.cal7; sub_array[8] = the_form.cal8; sub_array[9] = the_form.cal9; sub_array[10] = the_form.cal10; sub_array[11] = the_form.cal11; sub_array[12] = the_form.cal12; sub_array[13] = the_form.cal13; for(i = 0; i &lt; calorie_array.length; i++) { // Give subtemp the value or the calorie times the time. subtemp = (calorie_array[i] * time_array[i].value); // Put the converted string into the form field. sub_array[i].value = checkAmount(subtemp); // Add the converted number value to subtotal. subtotal += roundFloat(subtemp); } for(i = 0; i &lt; time_array.length; i++) { hours = (time_array[i].value); totalHrs += roundFloat(hours); } return subtotal; } function totalCalc() { var form; var subtotal; var total; var totalHrs; var hours; form = document.calc_form; // get the value of subtotal from totalCalc. subtotal = subCalc(form); totalHrs = subCalc(form); // Add the NUMBER values and subtotal. total = subtotal; // Convert this number into a string and display. form.total.value = checkAmount(total); // Adds total hours. hours = totalHrs form.total_hrs.value = checkAmount(hours); } function roundFloat(num) { num = parseFloat(num); num = Math.round(100*num)/100 return num } function checkAmount(num) { // Convert into a floating point number. num = parseFloat(num) // Round the number off. num = Math.round(100*num)/100 // Turn into a string. num = String(num) // Return the converted string. return num } //--&gt; &lt;/script&gt; </code></pre> <p>And the html:</p> <pre><code>&lt;form name="calc_form" id="form" method="post"&gt; &lt;table width="310" border="0" bgcolor="#EAEAEA"&gt; &lt;tr&gt; &lt;th width="95"&gt;&lt;h2&gt;Activity&lt;/h2&gt;&lt;/th&gt;&lt;th width="70"&gt;&lt;h2&gt;Time &lt;br&gt;(in hours)&lt;/h2&gt;&lt;/th&gt; &lt;th width="101"&gt;&lt;h2&gt;Calories used per hour&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;Sleeping&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt; &lt;p class="table-text"&gt; &lt;input name="sleeping_time" type="text" id="sleeping_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt; &lt;p class="table-text"&gt; &lt;input name="cal0" type="text" id="cal0" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Eating/Reading/&lt;br&gt;Watching TV&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="TV_time" type="text" id="TV_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal1" type="text" id="cal1" size="6" maxlength="6" /&gt; &lt;/span&gt;kcals&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Sitting&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="sit_time" type="text" id="sit_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal2" type="text" id="cal2" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Cooking&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="cook_time" type="text" id="cook_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal3" type="text" id="cal3" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Standing&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="stand_time" type="text" id="stand_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal4" type="text" id="cal4" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Washing &amp;amp; Dressing&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="wash_time" type="text" id="wash_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal5" type="text" id="cal5" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Walking Slowly&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="walk_S_time" type="text" id="walk_S_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal6" type="text" id="cal6" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Light housework&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="house_time" type="text" id="house_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal7" type="text" id="cal7" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Walking moderately&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="walk_M_time" type="text" id="walk_M_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal8" type="text" id="cal8" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Light gardening&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="gard_time" type="text" id="gard_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal9" type="text" id="cal9" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Dancing&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="danc_time" type="text" id="danc_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal10" type="text" id="cal10" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Walking up stairs&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="stairs_time" type="text" id="stairs_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal11" type="text" id="cal11" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Jogging&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="jog_time" type="text" id="jog_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal12" type="text" id="cal12" size="6" maxlength="6" /&gt; kcals&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="right"&gt;&lt;p class="table-text"&gt;Squash&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="sqsh_time" type="text" id="sqsh_time" size="2" maxlength="2" onchange="subCalc(document.calc_form);" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="cal13" type="text" id="cal13" size="6" maxlength="6" /&gt; kcals&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="70"&gt;&lt;p class="table-text"&gt;&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&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 = &lt;/strong&gt;&lt;/p&gt;&lt;/td&gt; &lt;td width="70"&gt;&lt;p class="table-text"&gt; &lt;input name="total_hrs" type="text" id="total_hrs" size="3" /&gt; hrs&lt;/p&gt;&lt;/td&gt; &lt;td width="101"&gt;&lt;p class="table-text"&gt; &lt;input name="total" type="text" id="total" size="6" /&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="2"&gt;&lt;input name="Calculate Total" type="button" id="Calculate Total" value="Calculate Total" onclick="totalCalc();" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload