嘿,我做了一个javascript计算器,并希望尝试5 5它给我55其他10?然而,如果我尝试5/5它给我1罚款,如果我尝试5 – 5它给我0罚款和相同的时间我不能忍受我的代码的问题也想尝试console.log它不工作,所以我使用document.write
这是我的代码
var Plus = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 + N2);
document.write(sum);
}
var Minus = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 - N2);
document.write(sum);
}
var Division = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 / N2);
document.write(sum);
}
var Times = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 * N2);
document.write(sum);
}
var Answer = prompt("Plus ?, Minus ?, Division ? or Times ?");
if( Answer === "Plus")
{
Plus();
}
else
{
if( Answer === "Minus")
{
Minus();
}
else
{
if( Answer === "Division")
{
Division();
}
else
{
if( Answer === "Times")
{
Times();
}
else
{
document.write("How did you get here?");
}
}
}
}
解决方法:
N1和N2是字符串,而不是数字.添加两个字符串将它们连接起来,这就是你的“错误”.
您需要将它们解析为数字:
var N1 = Number(prompt("Enter first number"));