**Write a function area that calculates the area of a circle. The function is given the radius of the circle. **
my code is this
let r;
let radius=r*r;
function area(r){
return (Math.PI.radius);
}
Error is showing that "area(0) does not return 0, but undefined." please explain this code.
I am new to this world please don't mind if you think this is one of the simple questions I have no one to ask except you guys!
Math.PI does not have a radius - it is a mathematical constant, which you have to multiply with the squared radius of the circle. Also you did not assign any value to r - so it is undefined. This one works:
function area(r) {
return Math.PI * r ** 2; /* r ** 2 is the JS notation of potenatiating a number */
}
console.log(area(0));
console.log(area(1));
console.log(area(5));
console.log(area(10));