MediumPro challengeTypeScript

Chain of Responsibility

TypeScriptDesign PatternsBehavioralChain of Responsibility

Pass a request along a chain of handlers until one of them handles it — the sender doesn't know (or care) which handler will actually process it.

Implement `Handler.handle(level)`: if this handler can handle the level, return its own name; otherwise forward to the next handler in the chain, or return 'unhandled' if there isn't one.

solve(levels) builds an InfoHandler → WarnHandler → ErrorHandler chain and runs every level through it.

solve(['error', 'info'])['ErrorHandler', 'InfoHandler']

Sample tests

Test #1Each level matches its own handler
Input: [["info","warn","error"]]
Output: ["InfoHandler","WarnHandler","ErrorHandler"]
Test #2Order of requests does not matter
Input: [["error","info"]]
Output: ["ErrorHandler","InfoHandler"]
Test #3No handler matches → unhandled
Input: [["debug"]]
Output: ["unhandled"]