我一直在制作一个 javascript 计算器,我可以获得所有基本函数和 pow() 函数,但我无法让它执行 Math.sqrt() 函数。在过去的几天里,我遇到了这个问题。这是我的代码,在此先感谢:
function calc()
{
var D = "";
var A = document.getElementById("num1").value;
var B = document.getElementById("op").value;
var C = document.getElementById("num2").value;
X = parseInt(A);
S = 2
var Z = "If you're seeing this, that means that the code isn't working!!"
D = eval(A + B + C);
if (B == "%")
{
D = ""
Z = Math.sqrt(X)*1
}
else if (B == "^")
{
D = ""
Z = Math.pow(X, C)
}
else if (B == "^2")
{
D = ""
Z = Math.pow(X, S)
}
else if (B == "")
{
D = "No Operator"
Z = ""
}
document.getElementById("result").value = D;
document.getElementById("sqrt-result").value = Z;
return false;
}