gpt4 book ai didi

javascript - 我正在尝试用 javascript 创建一个计算器。

转载 作者:行者123 更新时间:2023-12-03 03:50:34 25 4
gpt4 key购买 nike

我可以从用户那里获取输入,然后将其全部放入总和中,但它会返回 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!");
}

最佳答案

此代码片段中有几个问题。

  1. 如果需要数学加法,您需要转换 input1 和 input2。这些变量之前的+符号是一元运算符
  2. intput2 存在拼写错误。我应该输入input2
  3. 在 if 条件中使用 ===== 进行验证。 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!");
}

关于javascript - 我正在尝试用 javascript 创建一个计算器。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45181381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com