如何编写 javaScript 函数通过提示输入 2 个数字并在警报中显示这些数字的总和?

How to write a javaScript function to enter 2 numbers through prompts and display the sum of those numbers in an alert?

我想显示这个: 输入您的号码: 第一: 第二: 回答 : 我试过这样做:

var a = prompt("A : ", "");
var b = prompt("B : ", "");
function calc(a,b)
{
   return a + b;
}
alert ("The addition is =" + calc(a,b);

您只需要从提示中解析 return

var a = prompt("A : ", "");
var b = prompt("B : ", "");
function calc(a,b)
{
   return parseFloat(a) + parseFloat(b);
}
alert ("The addition is =" + calc(a,b));

只需一个提示即可完成:

var numbers = prompt("Enter two numbers separated by a semi-colon: ", "");
var a = numbers.split(";")[0];
var b = numbers.split(";")[1];

function calc(a,b)
{
   return parseFloat(a) + parseFloat(b);
}
alert ("The addition of " + numbers + " is = " + calc(a,b));