0
0
Node.jsframework~20 mins

Building URLs programmatically in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
Ahttps://example.com/path/subpath?query=123
Bhttps://example.com/subpath?query=123
Chttps://example.com/path?query=123
Dhttps://example.com/path/subpath
Attempts:
2 left
💡 Hint
Remember how the URL constructor resolves relative paths against the base URL.
component_behavior
intermediate
2: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());
Acolor=red,blue
Bcolor=blue
Ccolor=red
Dcolor=red&color=blue
Attempts:
2 left
💡 Hint
URLSearchParams allows multiple values for the same key by appending them.
📝 Syntax
advanced
2: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?
A
const url = new URL('https://api.example.com/data');
url.searchParams.set('id', id);
console.log(url.href);
B
const url = new URL('https://api.example.com/data?id=' + id);
console.log(url.href);
C
const url = new URL('https://api.example.com/data');
url.query.id = id;
console.log(url.href);
D
const url = new URL('https://api.example.com/data');
url.search.id = id;
console.log(url.href);
Attempts:
2 left
💡 Hint
Use the URLSearchParams API to set query parameters dynamically.
🔧 Debug
advanced
2: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);
ABecause the base URL must be an instance of URL, not a string.
BBecause the base URL is missing a trailing slash, so relative paths are invalid.
CBecause 'path' is a relative path without a leading slash, it cannot be resolved against the base string.
DBecause the URL constructor requires the first argument to be an absolute URL string.
Attempts:
2 left
💡 Hint
Check how relative paths without leading slashes are resolved against base URLs.
🧠 Conceptual
expert
3: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);
Ahttps://example.com/page?query=1#section2
Bhttps://example.com/page#section2?query=1
Chttps://example.com/page?query=1
Dhttps://example.com/page#section2
Attempts:
2 left
💡 Hint
The hash part of a URL comes after the query string and starts with '#'.