Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  176] [ 1]  / answers: 1 / hits: 18958  / 7 Years ago, sat, september 23, 2017, 12:00:00

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);

More From » area

 Answers
5

You 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.


[#56409] Wednesday, September 20, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patriciakiarrac

Total Points: 532
Total Questions: 100
Total Answers: 89

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
patriciakiarrac questions
;