Links in a single-page app are built from route patterns, like /users/:id/posts/:postId with { id: 42, postId: "a b" } giving /users/42/posts/a%20b.
Write buildPath(pattern, params). Split the pattern on / and ignore empty pieces. Keep plain segments as they are, and replace each :name segment with encodeURIComponent(String(params[name])). Join the segments with / and put a / in front; a pattern with no segments gives "/". If any parameter is missing from params, return null. Extra entries in params are ignored.
Input: pattern = "/users/:id/posts/:postId", params = {"id":42,"postId":"a b"} Output: "/users/42/posts/a%20b"
The number is turned into text and the space is encoded.
Input: pattern = "/users/:id", params = {} Output: null
id is missing.
1 to 200 characters and contain no *.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #736 Fill In a Route. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.