我可以从用户那里得到输入,我可以将它全部放入总和中,但它会重新调整NaN。
我的代码:
var input1 = prompt("input 1","0");
var operation = prompt("operation","+");
var input2 = prompt("input 2","0");
var ans = (input1 + intput2);
if (operation = "+")
{
document.write("input1 + intput 2 = " + ans);
}
else
{
document.write("Other operations coming soon!");
}
答案 0 :(得分:1)
此代码段中有几个问题。
+
符号是一元运算符intput2
有一个拼写错误。我应该input2
。==
或===
进行验证。 operation = "+"
只是分配值
var input1 = prompt("input 1", "0");
var operation = prompt("operation", "+");
var input2 = prompt("input 2", "0");
var ans = (+input1 + +input2);
if (operation == "+") {
document.write("input1 + intput 2 = " + ans);
} else {
document.write("Other operations coming soon!");
}

答案 1 :(得分:0)
提示返回字符串,因此您需要使用parseFloat()
将字符串转换为数字。并检查input2
中var ans
的咒语。
var input1 = prompt("input 1", "10");
var operation = prompt("operation", "+");
var input2 = prompt("input 2", "2");
var ans = parseFloat(input1) + parseFloat(input2);
if (operation = "+") {
document.write("input1 + intput 2 = " + ans);
} else {
document.write("Other operations coming soon!");
}