圆的面积

时间:2017-09-23 01:15:49

标签: javascript area

所以我创建了一个.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);

4 个答案:

答案 0 :(得分:2)

您需要将MyArea功能保留在calculateArea之外,并在calculateArea内拨打MyArea

调用MyArea函数而不是calculateArea

示例代码段:

&#13;
&#13;
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;
&#13;
&#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)

以下是单一功能解决方案:

&#13;
&#13;
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;
&#13;
&#13;