圆的周长为-
pi * (r * r)
圆的面积由-给出
2 * pi * r
其中r是圆的半径。
我们需要编写一个JavaScript函数,该函数接受圆的半径并计算圆的面积和周长之间的差
让我们为该函数编写代码-
const rad = 6; const circleDifference = radius => { const area = Math.PI * (radius * radius); const circumference = 2 * Math.PI * radius; const diff = Math.abs(area - circumference); return diff; }; console.log(circleDifference(rad)); console.log(circleDifference(5.25)); console.log(circleDifference(1));
输出结果
控制台中的输出-
75.39822368615503 53.60342465187584 3.141592653589793