Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Simple Router

Object-Oriented Programmingmedium

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 226: ES6 Classes

Build a tiny path router. Implement `Router`: - `addRoute(path, handler)` registers a handler name for an exact path. - `resolve(path)` returns the handler name, or `'404'` if the path is not registered.

Examples

Input: addRoute('/home','homeHandler'), addRoute('/about','aboutHandler'), resolve('/home'), resolve('/missing')
Output: ['homeHandler', '404']
Known paths resolve to their handler; unknown paths return '404'.

Sample tests

Input: register and resolve
Output: ["homeHandler","404"]
Input: override a route
Output: "two"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Returning undefined instead of '404' for unknown paths.
Approach & explanation (try first)

An object keyed by path maps to handler names; resolve returns the match or a 404 fallback.

Loading...
⌘/Ctrl + Enter

Run your code to see results.