因此,我创建了一个.js文件来计算圆的面积,然后calculateArea()需要对其进行计算。
它唯一要做的是提示符()。我究竟做错了什么?
function calculateArea(myRadius){
var area = (myRadius * myRadius * Math.PI);
return area;
function MyArea(){
calculateArea(myRadius);
alert("A circle with a " + myRadius +
"centimeter radius has an area of " + area +
"centimeters. <br>" + myRadius +
"represents the number entered by the user <br>" + area +
"represents circle area based on the user input.");
}
}
var myRadius = parseFloat(prompt("Enter the radius of your circle in cm:",0));
calculateArea(myRadius);
最佳答案
您需要将功能MyArea
保留在calculateArea
之外,并从calculateArea
内部调用MyArea
。
调用MyArea
函数而不是calculateArea
。
示例片段:
function calculateArea(myRadius) {
return (myRadius * myRadius * Math.PI);
}
function MyArea() {
var area = calculateArea(myRadius);
alert("A circle with a " + myRadius + "centimeter radius has an area of " + area + "centimeters. <br>" + myRadius + "represents the number entered by the user <br>" + area + "represents circle area based on the user input.");
}
var myRadius = parseFloat(prompt("Enter the radius of your circle in cm:", 0));
MyArea(myRadius);
PS:有更好的方法可以做到这一点。如有问题请发表评论。