Explorer
Node.js

Implement a `createChain()` with `addHandler(fn)` and `handle(request)`. Each handler can process the request or pass to the next.

Problem Statement

Implement a `createChain()` with `addHandler(fn)` and `handle(request)`. Each handler can process the request or pass to the next.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Chain of Responsibility passes a request through a chain of handlers. šŸ’” Hint 2: Each handler decides to process or pass to next. šŸ’” Hint 3: Handlers call next() to pass to the next handler in the chain. āœ… Solution: ```javascript function createChain() { const handlers = []; return { addHandler(fn) { handlers.push(fn); }, handle(request) { let index = 0; function next() { if (index < handlers.length) { handlers[index++](request, next); } } next(); return request; } }; } ```

Chain of Responsibility

Hard
Implement a `createChain()` with `addHandler(fn)` and `handle(request)`. Each handler can process the request or pass to the next.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[{"value":5,"handler":"small"},{"value":50,"handler":"medium"},{"value":500,"handler":"large"}]