简体   繁体   English

如何解决负数的问题?

[英]How can I fix the problem with negative numbers?

I've been doing Asabeneh 30 day JavaScript roadmap and in day 7, level 2, there is an exercise that asks us to build a quadratic function solver.我一直在做 Asabeneh 30 天 JavaScript 路线图,在第 7 天第 2 级,有一个练习要求我们构建一个二次 function 求解器。 I've tried my function with a couple of numbers and seems to work out just fine, but when plugging in some other numbers (for instance, periodic numbers as roots) the function just outputs two random values我已经用几个数字尝试了我的 function 并且似乎工作得很好,但是当插入一些其他数字(例如,作为根的周期数)时,function 只输出两个随机值

Example:例子:

 function solveQuadEquation (a = 0, b = 0, c = 0) { let x1 = ((-1 * b + Math.sqrt(b * b - 4 * a * c)) / 2 * a) let x2 = ((-1 * b - Math.sqrt(b * b - 4 * a * c)) / 2 * a) let discriminant = Math.pow(b, 2) - 4 * a * c if (discriminant > 0) { return 'The function has two solutions: ' + x1 + ', ' + x2 } else if (discriminant === 0) { return 'The function has two repeated roots: ' + x1 } else { return 'the function has no real values' } return [x1, x2]; } console.log(solveQuadEquation(9, 3, -4))

The console outputs控制台输出

// The function has two real values: 42.161192594... , -69.161192594... // function 有两个实数值:42.161192594...,-69.161192594...

any ideas on what might be wrong with it?关于它可能有什么问题的任何想法?

thanks!!谢谢!!

The quadratic formula is (-b +/- sqrt(delta))/2a二次公式为(-b +/- sqrt(delta))/2a
Your code does a*(-b +/- sqrt(delta))/2您的代码执行a*(-b +/- sqrt(delta))/2
Watch your brackets next time.下次注意你的括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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