Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are many things to correct here.</p> <ul> <li><p>First, you want to get the value of all your inputs inside the print answer function. Otherwise, your variables are initialized when the page is loaded, and the fields are not filled yet. move your code around like this</p> <pre><code>function printAnswer() { var volts = document.getElementsByName("voltDrop_volts")[0].value; var acdc = document.getElementsByName("voltDrop_acdc")[0].value; var amps = document.getElementsByName("voltDrop_amps")[0].value; var length = parseFloat(document.getElementsByName("voltDrop_ft")[0].value); var awg = document.getElementsByName("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); console.log(volts, acdc, amps, length, awg, ohm); document.getElementById("theAnswer").innerHTML = vFinal; } </code></pre></li> <li><p>Also, keep in mind that what you get from a field is a string, so you might have to convert the values to numbers there, using parseInt() or parseFloat().</p></li> </ul> <p>e.g: <code>var length = parseFloat(document.getElementsByName("voltDrop_ft")[0].value);</code></p> <ul> <li>Similarly, when you compare using ===, you want to compare objects of the same type. So if <code>awg</code> is a string, as you get it from th HTML, compare it to a string:</li> </ul> <p>e.g: <code>if (awg === '6') { ... }</code></p> <ul> <li><p>You can use <code>document.getElementsByName</code> or <code>document.getElementById</code> but bear in mind that the first returns a list, so you want to use [0], when the second one returns only one element. Also, for the second one to work, you will need you fields to have id set.</p></li> <li><p>Debug using the devtools and web console. Those are your best friends for this kind of debugging.</p></li> </ul>
    singulars
    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