Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a calculator in JS and getting an undefined. Beginner Programer
    primarykey
    data
    text
    <p>So I'm creating a calculator that does a pretty simple calculation. Just some simple math. However there is one property that has a lot of different variables. The original program was written in jQuery so I had to redo the properties to work with our sites regular Javascript framework. I think it's a problem with one of the declared variables. If you run the program, you get the answer "undefined" like it's missing a variable.</p> <pre><code>&lt;script type="text/javascript"&gt; var volts = document.getElementById("voltDrop_volts")[0].value; var acdc = document.getElementById("voltDrop_acdc")[0].value; var amps = document.getElementById("voltDrop_amps")[0].value; var length = document.getElementById("voltDrop_ft")[0].value; var awg = document.getElementById("voltDrop_awg")[0].value; var ohm = 0; if (awg === 36) { ohm = 371.0; } else if (awg === 34) { ohm = 237.0; } else if (awg === 32) { ohm = 164.0; } else if (awg === 30) { ohm = 103.0; } else if (awg === 28) { ohm = 64.9; } else if (awg === 26) { ohm = 37.3; } else if (awg === 24) { ohm = 23.3; } else if (awg === 22) { ohm = 14.7; } else if (awg === 20) { ohm = 10.0; } else if (awg === 18) { ohm = 6.4; } else if (awg === 16) { ohm = 4.0; } else if (awg === 14) { ohm = 2.5; } else if (awg === 12) { ohm = 1.6; } else if (awg === 10) { ohm = 1.1; } else if (awg === 8) { ohm = .62; } else if (awg === 6) { ohm = .40; } else if (awg === 4) { ohm = .24; } else if (awg === 2) { ohm = .16; } else if (awg === 1) { ohm = .13; } else if (awg === '1/0') { ohm = .096; } else if (awg === '2/0') { ohm = .077; } else if (awg === '3/0') { ohm = .062; } else if (awg === '4/0') { ohm = .049; } else { msg = "Error"; } var vDrop = ((ohm * 2) * (length / 1000) * amps); var vFinal = (volts - vDrop); function formReset() { document.getElementById("form1").reset(); } function printAnswer() { document.getElementById("theAnswer").innerHTML = vFinal; } &lt;/script&gt; </code></pre> <p>I'm gonna talk through this so hopefully you can understand how I coded this. The first var is a dropdown with 3 options. The second is a dropdown with 2 options that don't affect the calculation. The third is a user input that should work as long as the user inputs any number. The fourth is a length inputed by a user. Should also work with any number. The fifth is where I think something is getting messed up. Its a number as well but it has to be one of the numbers in the if/else table.</p> <p>Personally I think I didn't code the if/else stuff correctly. Should the <code>var</code> element for <code>ohm</code> contain the if/else statement? Any help is greatly appreciated. I don't work in Javascript but my boss dumped this on me and said figure it out and this is where I ended up.</p> <p>Thanks!</p> <p>EDIT: HTML for those interested</p> <pre><code> &lt;body&gt; &lt;h1&gt;Voltage Drop Calculator Tool&lt;/h1&gt; &lt;form id=form1&gt; &lt;div class="calculator-wrapper" id="voltageDropCalc" style="margin: 10px 0 20px 20px; padding: 15px;"&gt;&lt;!-- BEGIN Calc wrapper --&gt; &lt;p&gt;1. Initial Voltage&lt;/p&gt; &lt;select name="voltDrop_volts" size="1" style="padding: 3px;"&gt; &lt;option&gt;Choose...&lt;/option&gt; &lt;option value="12"&gt;12 Volt&lt;/option&gt; &lt;option value="24"&gt;24 Volt&lt;/option&gt; &lt;/select&gt; &lt;p&gt;2. AC / DC&lt;/p&gt; &lt;select class="sized" name="voltDrop_acdc" size="1" style="padding: 3px;"&gt; &lt;option selected="selected" value="dc"&gt;DC&lt;/option&gt; &lt;option value="ac"&gt;AC&lt;/option&gt; &lt;/select&gt; &lt;p class="field-label field-label-right"&gt;3. Current (Amps)&lt;/p&gt; &lt;input align="bottom" class="sized" name="voltDrop_amps" size="15" style="padding: 3px;" type="text" /&gt; &lt;p class="field-label field-label-right"&gt;4. Cable Length (ft)&lt;/p&gt; &lt;input align="bottom" class="sized" name="voltDrop_ft" size="15" style="padding: 3px;" type="text" /&gt; &lt;p class="field-label field-label-right"&gt;5. Cable Gauge (AWG)&lt;/p&gt; &lt;input align="bottom" class="sized" name="voltDrop_awg" size="15" style="padding: 3px;" type="text" /&gt; &lt;p&gt;&lt;a href="#" id="voltageDropSubmit" style="text-decoration: none;" onclick="printAnswer()"&gt;&lt;button type="button"&gt;Calculate&lt;/button&gt;&lt;/a&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a class="clearBtn" href="#" style="text-decoration: none;"&gt;&lt;button type="button" onclick="formReset()"&gt;Cancel&lt;/button&gt;&lt;/a&gt;&lt;/p&gt; &lt;div class="results-box"&gt; &lt;p id="theAnswer"style="margin: 10px 0;"&gt;&amp;nbsp;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt; &lt;p&gt;Note: Industry standard is a voltage drop of no greater than 10%.&lt;/p&gt; &lt;div class="errorMsg"&gt;&lt;/div&gt; &lt;!-- displays error messages to user --&gt; &lt;/div&gt; &lt;!-- END Calc wrapper --&gt; &lt;/body&gt; </code></pre>
    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.
 

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