简体   繁体   中英

JavaScript returning an answer only sometimes

I have been exploring JavaScript and been working on making a working calculator that solves quadratic equations for both answers. I have been running into the issue that sometimes the formula returns NaN when I know the real answer. Such as using 1 for b, -5 for a, and -14 for c returns "NaN, NaN" but should be 7,-2. Is there a reason for this or is there a bug in my code? Thanks in advance

 function quadForm() { var b = document.getElementById("oppb").value; var a = document.getElementById("a").value; var c = document.getElementById("c").value; var ansplsunrd = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); var ansmnsunrd = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); document.getElementById("result").innerHTML = ansplsunrd + ", " + ansmnsunrd; }
 <h2>Quadratic Calculator</h2> <p class="var-1">b:</p><input type="number" class="calc-field" id="oppb" placeholder="Insert B" value="0"> <p class="var-2">a:</p><input type="number" class="calc-field" id="a" placeholder="Insert A" value="0"> <p class="var-3">c:</p><input type="number" class="calc-field" id="c" placeholder="Insert C" value="0"> <br> <br> <button type="button" onClick="quadForm()">Calculate</button> <p id="result">Result shown here</p>

I figured out how to correct my errors and make my code more clean looking by adding variables for each section of the equation. I separated the process of making b opposite, division of 2a, and power of b^2-4ac. Then making an if/ else statement in the snippet. Thanks to those who pointed out the issue of a negative discriminant that is what got me here. I just need to add an "i" to the answer for accuracy.

 function quadForm() { var b = document.getElementById("oppb").value; var a = document.getElementById("a").value; var c = document.getElementById("c").value; var discrim = Math.pow(b, 2) - (4 * a * c); var divby = 2*a; var oppb = -1*b; if (discrim < 0) { var negrt = (oppb + Math.sqrt((discrim*-1)))/(divby); var posrt = (oppb - Math.sqrt((discrim*-1)))/(divby); document.getElementById("result").innerHTML = negrt + "i" + ", " + posrt + "i"; } else { var ansplsunrd = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); var ansmnsunrd = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a); document.getElementById("result").innerHTML = ansplsunrd + ", " + ansmnsunrd; } }
 <h2>Quadratic Calculator</h2> <p class="var-1">b:</p><input type="number" class="calc-field" id="oppb" placeholder="Insert B" value="1"> <p class="var-2">a:</p><input type="number" class="calc-field" id="a" placeholder="Insert A" value="-5"> <p class="var-3">c:</p><input type="number" class="calc-field" id="c" placeholder="Insert C" value="-14"> <br> <br> <button type="button" onClick="quadForm()">Calculate</button> <p id="result">Result shown here</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM