我尝试编写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>
答案 0 :(得分:0)
你需要:
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)
修复了您的代码。
这里发生了什么变化:
math.pi
必须大写为Math.PI
正如您将提示存储到myRadius
变量一样,您还需要将calculateArea()
的结果存储到变量中。我在完成运行后将其设置为myArea
。
// 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;
答案 2 :(得分:0)
您将返回myArea
但不将其存储在某处,而 Math.PI 则不是math.pi.当myRadius
全局定义时,您也可以在其他函数中使用它。
var myArea = calculateArea(myRadius); // gets the return myArea value and defined globally
<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;