JavaScript: SyntaxError missing ) after argument list

喜欢而已 提交于 2019-12-25 05:28:18

问题


var quadraticFormula = function(a, b, c) {

        console.log((-b + sqrt( (b*b) - 4 * a * c)) / 2a) };

quadraticFormula(2,2,2)

I am a beginner trying to make a simple quadratic equation calculator on javascript. I keep on getting a syntax error message saying "missing ) after argument list". What is wrong with my code?


回答1:


Try adding a * sign in 2a:

var quadraticFormula = function(a, b, c) {

        console.log((-b + Math.sqrt( (b*b) - 4 * a * c)) / (2*a));

};

Also sqrt is part of Math so invoke it with Math.sqrt. Notice that quadraticFormula(2,2,2) will print NaN as it will try to do the square root of a negative number: (2*2) - 4 * 2 * 2.

Edit:
I wrapped 2*a inside () to correct the quadratic formula.



来源:https://stackoverflow.com/questions/31171211/javascript-syntaxerror-missing-after-argument-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!