Here's a basic JavaScript script that demonstrates a simple function to calculate the area of a circle given a radius.
function calculateCircleArea(radius) {
if (radius < 0) {
return "Radius cannot be negative";
}
var area = Math.PI * Math.pow(radius, 2);
return "The area of the circle is: " + area.toFixed(2);
}
Users can call this function with a radius value, and it will return the area of the circle with that radius. For example:
javascript
console.log(calculateCircleArea(5));
This code will output: "The area of the circle is: 78.54" (rounded to 2 decimal places). If the input is a negative number, it will return "Radius cannot be negative".
function calculateCircleArea(radius) {
if (radius < 0) {
return "Radius cannot be negative";
}
var area = Math.PI * Math.pow(radius, 2);
return "The area of the circle is: " + area.toFixed(2);
}
Users can call this function with a radius value, and it will return the area of the circle with that radius. For example:
javascript
console.log(calculateCircleArea(5));
This code will output: "The area of the circle is: 78.54" (rounded to 2 decimal places). If the input is a negative number, it will return "Radius cannot be negative".