如何将变量和函数传递给警报功能?

时间:2017-09-17 19:15:59

标签: javascript html

我尝试编写JavaScript,提示用户输入数据,运行函数对该数据执行计算,然后创建警报以显示该数据。

目前,此代码有两个函数:calculateArea和displayArea:

calculateArea

function calculateArea(myRadius) {

    //sets the variable 'myArea' to radius^2 * pi
    var myArea = (myRadius * myRadius * math.pi);

    //returns the variable myArea to the global function.
    return myArea;

}

displayArea

function displayArea() {

    //the code for the alert
    alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters.");

}

预期输出:网页应提示用户输入半径,然后显示一个显示区域计算的提醒。

实际输出:网页会提示用户输入半径,而不做任何其他操作。

这是HTML文档的正文:

<body>

<script>

// Performs the calculateArea function on the myRadius variable.
function calculateArea(myRadius) {

    //sets the variable 'myArea' to radius^2 * pi
    var myArea = (myRadius * myRadius * math.pi);

    //returns the variable myArea to the global function.
    return myArea;

}

//Performs an alert that shows the area and radius of the circle.    
function displayArea() {

    //the code for the alert
    alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters.");

}

//prompts the user for the circle radius.
var myRadius = parseFloat(prompt("Enter the radius of your circle in centimeters: ",));

//runs the calculateArea function on the myRadius variable entered by the user.
calculateArea(myRadius);

//runs the displayArea function, showing the alert.
displayArea();

</script>


</body>

3 个答案:

答案 0 :(得分:0)

你需要:

  • 使用准确的uppper /小写字符编写Math.PI
  • 从某个变量
  • 中获取计算结果
  • 将变量传递给第二个函数,避免依赖全局变量。

以下是它的工作原理:

function calculateArea(myRadius) {
    // Math needs a capital, and PI is all caps
    var myArea = myRadius * myRadius * Math.PI;
    return myArea;
}

// Add arguments to this function:
function displayArea(myRadius, myArea) {
    alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters.");
}

var myRadius = parseFloat(prompt("Enter the radius of your circle in centimeters: ",));

// store the result
var myArea = calculateArea(myRadius);
displayArea(myRadius, myArea); // pass the input and result that this function needs

答案 1 :(得分:0)

修复了您的代码。

这里发生了什么变化:

  1. math.pi必须大写为Math.PI

  2. 正如您将提示存储到myRadius变量一样,您还需要将calculateArea()的结果存储到变量中。我在完成运行后将其设置为myArea

  3. &#13;
    &#13;
    // Performs the calculateArea function on the myRadius variable.
    function calculateArea(myRadius) {
    
      //sets the variable 'myArea' to radius^2 * pi
      var myArea = (myRadius * myRadius * Math.PI);
    
      //returns the variable myArea to the global function.
      return myArea;
    
    }
    
    //Performs an alert that shows the area and radius of the circle.    
    function displayArea() {
    
      //the code for the alert
      alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters.");
    
    }
    
    //prompts the user for the circle radius.
    var myRadius = parseFloat(prompt("Enter the radius of your circle in centimeters: ", ));
    
    //runs the calculateArea function on the myRadius variable entered by the user.
    var myArea = calculateArea(myRadius);
    
    //runs the displayArea function, showing the alert.
    displayArea();
    &#13;
    &#13;
    &#13;

答案 2 :(得分:0)

您将返回myArea但不将其存储在某处,而 Math.PI 则不是math.pi.当myRadius全局定义时,您也可以在其他函数中使用它。

var myArea = calculateArea(myRadius); // gets the return myArea value and defined globally

&#13;
&#13;
<body>

<script>
  
// Performs the calculateArea function on the myRadius variable.
function calculateArea(myRadius) {

    //sets the variable 'myArea' to radius^2 * pi
    var myArea = (myRadius * myRadius * Math.PI);

    //returns the variable myArea to the global function.
    return myArea;

}

//Performs an alert that shows the area and radius of the circle.    
function displayArea() {

    //the code for the alert
    alert("A circle with a " + myRadius + " centimeter radius has an area of " + myArea + " centimeters.");

}

//prompts the user for the circle radius.
var myRadius = parseFloat(prompt("Enter the radius of your circle in centimeters: ",));

//runs the calculateArea function on the myRadius variable entered by the user.
var myArea = calculateArea(myRadius);

//runs the displayArea function, showing the alert.
displayArea();

</script>
&#13;
&#13;
&#13;