HardPro challengeTypeScript

Visitor

TypeScriptDesign PatternsBehavioralVisitor

Add new operations over a set of classes without modifying those classes — an AreaVisitor and a PerimeterVisitor can both operate on CircleShape/SquareShape without either shape knowing about area or perimeter math.

Implement `CircleShape.accept`/`SquareShape.accept` (call the matching visitCircle/visitSquare on the visitor) and AreaVisitor/PerimeterVisitor.

solve(shapes, visitorType) builds each shape from { type: 'circle', radius } | { type: 'square', side }, visits it, and returns the results (round circle results to 2 decimals).

Sample tests

Test #1Square area
Input: [[{"side":4,"type":"square"}],"area"]
Output: [16]
Test #2Circle area, rounded to 2 decimals
Input: [[{"type":"circle","radius":2}],"area"]
Output: [12.57]
Test #3Mixed shapes, perimeter visitor
Input: [[{"type":"circle","radius":2},{"side":3,"type":"square"}],"perimeter"]
Output: [12.57,12]