MediumPro challengePythonJavaScriptTypeScript

URL Builder

Node.jsURLStrings

Build a URL from components: { protocol, host, port?, path?, query? }.

  • Always include protocol://host.
  • Port appended only if present.
  • Path defaults to '/'.
  • Query: object → URL-encoded query string with sorted keys; skip keys whose

value is null.

Sample tests

Test #1Defaults to /
Input: [{"host":"example.com","protocol":"https"}]
Output: "https://example.com/"
Test #2With port and path
Input: [{"host":"localhost","path":"/api","port":3000,"protocol":"http"}]
Output: "http://localhost:3000/api"
Test #3With query
Input: [{"host":"x.com","query":{"a":1,"b":2},"protocol":"https"}]
Output: "https://x.com/?a=1&b=2"
Test #4Query encoded
Input: [{"host":"x.com","query":{"q":"hi there"},"protocol":"https"}]
Output: "https://x.com/?q=hi%20there"
Test #5Skip null
Input: [{"host":"x.com","query":{"a":null,"b":1},"protocol":"https"}]
Output: "https://x.com/?b=1"