Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I changed the sum line to</p> <pre><code>distanceTravelled += distances[i]; </code></pre> <p>I also swapped the dynamic values with temporary ones, it displayed <code>$150</code>, which is the expected result (and nothing to do with brackets as mentioned by @cbp).</p> <pre><code>function calcRate() { var engineRate = 10; var distanceTravelled = 0.0; var distances = new Array(); distances[0] = 1; distances[1] = 2; distances[2] = 3; distances[3] = 4; distances[4] = 5; for (var i = 0; i &lt; distances.length; i++) { if (!isNaN(distances[i])) { distanceTravelled += distances[i]; } } alert("$" + distanceTravelled * engineRate); } calcRate(); </code></pre> <p><a href="http://jsfiddle.net/tcEpF/2/" rel="nofollow">http://jsfiddle.net/tcEpF/2/</a></p> <p>I then swapped in the jQuery selector for the output text box, it displays <code>$150</code>:</p> <pre><code>&lt;input name="reimbursement" /&gt; </code></pre> <p><a href="http://jsfiddle.net/tcEpF/3/" rel="nofollow">http://jsfiddle.net/tcEpF/3/</a></p> <p>Assuming the engine rate are radio buttons, I proceeded to add them in:</p> <pre><code>&lt;input type="radio" name="enginesize" value="10" /&gt;10&lt;br&gt; &lt;input type="radio" name="enginesize" value="100" /&gt;100&lt;br&gt; &lt;input type="radio" name="enginesize" value="1000" /&gt;1000&lt;br&gt; </code></pre> <p>Clicking on any of them displays the expected results: $150, $1500, $15000</p> <p><a href="http://jsfiddle.net/tcEpF/4/" rel="nofollow">http://jsfiddle.net/tcEpF/4/</a></p> <p><strong>I can only conclude that the selector for <code>field_Distance</code> is wrong:</strong></p> <pre><code>jQuery("#field_Distance1") </code></pre> <p>You might want to use this instead to get the name of the field, as <code>#</code> means you are getting the ID attribute instead, which is probably missing.</p> <pre><code>jQuery("input[name=field_Distance1]") </code></pre> <p>Why is it displaying <strong>$0</strong> then? As the selector is wrong, the array now contains <code>NaN</code> values. distanceTravelled remains as <code>0.0</code> after the for-loop as <code>!isNaN(distances[i])</code> returns false, skipping over the sum. In the end, anything multiplied by 0 returns 0.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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