← Back to challenges

Solve for the Red Area

JavaScriptHardalgorithmsmathnumbers

Instructions

This problem went viral in China, spreading on Weibo. The problem is to solve for the area shown in red between the semicircle and the rectangle’s diagonal.

Create a function that takes a number r as an the length of the side and returns the area rounded to the nearest thousandth.

The shaded part

Examples

redArea(0) ➞ 0

redArea(4) ➞ 1.252

redArea(25) ➞ 48.906

Notes

The input r is always a valid number.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.