MediumPro challengeJavaScriptTypeScript

Liskov Substitution Principle

Clean CodeSOLIDLSP

Subtypes must be substitutable for their base type without breaking behavior.

// dirty — Square breaks Rectangle's contract
class Rectangle {
  setWidth(w) { this.width = w; }
  setHeight(h) { this.height = h; }
  area() { return this.width * this.height; }
}
class Square extends Rectangle {
  setWidth(w) { this.width = w; this.height = w; } // breaks LSP!
  setHeight(h) { this.width = h; this.height = h; }
}

Refactor using factory functions so createRectangle and createSquare are independent — no inheritance. Both expose area().

solve('square', 5)25 (side² )
solve('rect', 4, 6)24 (width × height)

Sample tests

Test #1Rectangle 4×6
Input: ["rect",4,6]
Output: 24
Test #2Square side 5
Input: ["square",5]
Output: 25
Test #3Square-sized rectangle still independent
Input: ["rect",3,3]
Output: 9