所以我创建了一个.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);
答案 0 :(得分:2)
您需要将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);
&#13;
PS:有更好的方法可以做到这一点。如有疑问,请发表意见。
答案 1 :(得分:1)
这是一种计算圆面积的方法
let Area, Environment;
let Radius = prompt("Enter Radius ");
function calculate(Radius) {
CalEnvironment(Radius);
CalArea(Radius);
}
function CalEnvironment(Radius) {
Environment = Radius * 3.14 * 2;
console.log("Environment is : " + Environment);
}
function CalArea(Radius) {
Area = Radius * Radius * 3.14;
console.log("Area is : " + Area);
}
calculate(Radius);
答案 2 :(得分:0)
你基本上需要在MyArea
之外拨打calculateArea
,但在这种情况下,为什么不这样呢?
function calculateArea(myRadius) {
return myRadius * myRadius * Math.PI;
}
var myRadius = parseFloat(prompt("Enter the radius of your circle in cm:",0));
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.");
答案 3 :(得分:0)
以下是单一功能解决方案:
function MyArea(myRadius){
var area = Math.pow(myRadius, 2) * Math.PI;
alert(
"A circle with a " + myRadius +
"centimeter radius has an area of " +
area + "centimeters. \n" + myRadius +
"represents the number entered by the user \n" +
area + "represents circle area based on the user input."
);
}
var myRadius = parseFloat(prompt("Enter the radius of your circle in cm:", 0));
MyArea(myRadius);
&#13;