[해결로 표시] JavaScript에서 주어진 반지름으로 원의 넓이를 계산하는 방법은 무엇인가요?

  

4
주제 스타터

I must write a JavaScript function that calculates circle area by given radius. Print the area as it is calculated and then print it rounded to two decimal places.

  • The input comes as a single number argument - the radius of the circle.
  • The output should be printed to the console on a new line for each result.

Example:

입력: 5

출력:
78.53981633974483
78.54

2개 답글
3

My code is:

function circleArea(radius) {
    let area = Math.PI * (radius * radius);
    console.log(area);
    console.log(Math.round(area*100)/100);
}
 
circleArea(5);
1

Two solutions from me:

function circleA(r) {
    console.log(Math.PI * r * r);
    console.log(Math.round(Math.PI * r * r * 100) / 100);
}
circleA(5);
function cA(rad) {
    let area = Math.PI * rad * rad;
    console.log(area);
 
    let areaRounded = Math.round(area * 100) / 100;
    console.log(areaRounded);
}
 
cA(5);
공유: