← Back to challenges

Area of Overlapping Rectangles

JavaScriptHardalgebramathobjectsgeometry

Instructions

Create a function that returns the area of the overlap between two rectangles. The function will receive two rectangles, each with the coordinates of two of its opposite angles.

Examples

overlappingRectangles(
  [{ x: 2, y: 1 }, { x: 5, y: 5 }],
  [{ x: 3, y: 2 }, { x: 5, y: 7 }]
) ➞ 6

overlappingRectangles(
  [{ x: 2, y: -9 }, { x: 13, y: -4 }],
  [{ x: 5, y: -11 }, { x: 7, y: -2 }]
) ➞ 10

overlappingRectangles(
  [{ x: -8, y: -7 }, { x: -4, y: 0 }],
  [{ x: -5, y: -9 }, { x: -1, y: -2 }]
) ➞ 5

Example 1

Example 2

Example 3

Notes

  • Coordinates can be positive or negative integers.
  • Not all examples have overlaps.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.