最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Quadratic Equation Solver in JavaScript - Stack Overflow

programmeradmin7浏览0评论

For some reason, when a=1, b=1, c=-1, I am not getting the desired result of -1.6180339887499 and 0.61803398874989. Instead, I get 2 and 1. What am I doing wrong?

function solve(a,b,c){
    var result = (((-1*b) + Math.sqrt(Math.pow(b,2)) - (4*a*c))/(2*a));
    var result2 = (((-1*b) - Math.sqrt(Math.pow(b,2)) - (4*a*c))/(2*a));
    
    return result + "<br>" + result2;
}

document.write( solve(1,1,-1) );

For some reason, when a=1, b=1, c=-1, I am not getting the desired result of -1.6180339887499 and 0.61803398874989. Instead, I get 2 and 1. What am I doing wrong?

function solve(a,b,c){
    var result = (((-1*b) + Math.sqrt(Math.pow(b,2)) - (4*a*c))/(2*a));
    var result2 = (((-1*b) - Math.sqrt(Math.pow(b,2)) - (4*a*c))/(2*a));
    
    return result + "<br>" + result2;
}

document.write( solve(1,1,-1) );

Share Improve this question edited Nov 1, 2018 at 5:52 Pang 10.1k146 gold badges86 silver badges124 bronze badges asked Oct 31, 2015 at 17:27 StardustStardust 1,0293 gold badges13 silver badges24 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9

You need another grouping:

var result = (((-1 * b) + Math.sqrt(Math.pow(b, 2)) - (4 * a * c)) / (2 * a));  // wrong
var result2 = (((-1 * b) - Math.sqrt(Math.pow(b, 2)) - (4 * a * c)) / (2 * a)); // wrong

vs

var result = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);      // right
var result2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);     // right

All together:

function solve(a, b, c) {
    var result = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    var result2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    return result + "<br>" + result2;
}
document.write(solve(1, 1, -1));

Try

var a, b, c, discriminant, root1, root2, r_Part, imag_Part;

document.write(realpart ="+r_Part" and imaganary part ="+imag_Part");
discriminant = b*b-4*a*c;


if (discriminant > 0)
{
    root1 = (-b+sqrt(discriminant))/(2*a);
    root2 = (-b-sqrt(discriminant))/(2*a);

document.write(real part ="+r_Part" and imaganary part ="+imag_Part");   
}

else if (discriminant == 0)
{
    root1 = root2 = -b/(2*a);
   document.write(real part ="+r_Part" and imaganary part ="+imag_Part");
}


else
{
    r_Part = -b/(2*a);
    imag_Part = sqrt(-discriminant)/(2*a);
    document.write(real part ="+r_Part" and imaganary part ="+imag_Part");
}
function solve(a, b, c) {
    var result = ((-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a)).toFixed(3);
    var result2 = ((-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a)).toFixed(3);
    return "{"+result + "," + result2+"}";
}
document.write(solve(1, -4, -7));

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <link rel="stylesheet" href="">
    </head>
    <body>
        <script type="text/javascript">
        var a=2;
            var b=9;
            var c=2;
    
            var root_part=Math.sqrt(b*b-4*a*c);
            var denom=2*a;
            
            if (isNaN(root_part) === true){
                document.write("<br> Impossible to solve");
            }
            else{
            var root1=(-b+root_part)/denom;
            var root2=(-b-root_part)/denom;
            document.write("<br>First Root is="+root1+ " and Second Root is="+root2); 
            }
        </script>
    </body>
    </html>

发布评论

评论列表(0)

  1. 暂无评论