Challenge - 5 Problems
URL Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this URL construction code?
Consider the following Node.js code using the URL class. What will be logged to the console?
Node.js
const { URL } = require('url');
const base = 'https://example.com/path';
const url = new URL('/subpath?query=123', base);
console.log(url.href);Attempts:
2 left
💡 Hint
Remember how the URL constructor resolves relative paths against the base URL.
✗ Incorrect
When the second argument to the URL constructor starts with a slash, it replaces the path of the base URL entirely. So '/subpath' replaces '/path', resulting in 'https://example.com/subpath?query=123'.
❓ component_behavior
intermediate2:00remaining
How does URLSearchParams handle multiple values for the same key?
Given the following code, what will be the output of the console.log statement?
Node.js
const params = new URLSearchParams(); params.append('color', 'red'); params.append('color', 'blue'); console.log(params.toString());
Attempts:
2 left
💡 Hint
URLSearchParams allows multiple values for the same key by appending them.
✗ Incorrect
Appending multiple values for the same key results in repeated key-value pairs in the query string, separated by '&'.
📝 Syntax
advanced2:00remaining
Which option correctly creates a URL with a dynamic query parameter?
You want to create a URL 'https://api.example.com/data' with a query parameter 'id' set to a variable value. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Use the URLSearchParams API to set query parameters dynamically.
✗ Incorrect
Option A uses the URL class and its searchParams property correctly to set the query parameter. Option A concatenates strings but does not encode properly. Options A and D use invalid properties.
🔧 Debug
advanced2:00remaining
Why does this URL constructor code throw an error?
Examine the code below. Why does it throw a TypeError?
Node.js
const { URL } = require('url');
const base = 'https://example.com';
const url = new URL('path', base);
console.log(url.href);Attempts:
2 left
💡 Hint
Check how relative paths without leading slashes are resolved against base URLs.
✗ Incorrect
The URL constructor expects relative paths to start with '/' or be absolute URLs. 'path' without a leading slash is treated as a relative path to the current directory, which is invalid here and causes a TypeError.
🧠 Conceptual
expert3:00remaining
What is the effect of setting url.hash in a URL object?
Given the code below, what will be the value of url.href after setting url.hash = '#section2'?
Node.js
const { URL } = require('url');
const url = new URL('https://example.com/page?query=1');
url.hash = '#section2';
console.log(url.href);Attempts:
2 left
💡 Hint
The hash part of a URL comes after the query string and starts with '#'.
✗ Incorrect
Setting url.hash adds or replaces the fragment identifier at the end of the URL, after the query string.