So I created a .js file to calculate the area of a circle and calculateArea() needs to calculate it. The only thing that it does is the prompt(). What am I doing wrong?
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);
So I created a .js file to calculate the area of a circle and calculateArea() needs to calculate it. The only thing that it does is the prompt(). What am I doing wrong?
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);
Share
Improve this question
edited Sep 29, 2017 at 8:51
teppic
7,3061 gold badge33 silver badges38 bronze badges
asked Sep 23, 2017 at 1:15
ma_rodriguesma_rodrigues
331 gold badge1 silver badge4 bronze badges
2
- *Not calling function MyArea, You need to revisit function calculateArea . – Ajay Narain Mathur Commented Sep 23, 2017 at 1:18
- As an aside, area is not measured in centimetres. (Your alert should probably say "square centimetres".) – nnnnnn Commented Sep 23, 2017 at 1:28
4 Answers
Reset to default 2You need to keep function MyArea
outside calculateArea
and call calculateArea
from within MyArea
.
Call MyArea
function instead of calculateArea
.
Example Snippet:
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: There are better ways to do this. Comment in case of questions.
this is a way for calculate the circle area
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);
You basically need to call MyArea
outside of calculateArea
but in this case, why not something like this?
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.");
The following is a single function solution:
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);