Challenge - 5 Problems
URL Resolution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of resolving a relative URL against a base URL?
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 = new URL('https://example.com/folder/page.html');
const relative = new URL('../image.png', base);
console.log(relative.href);Attempts:
2 left
💡 Hint
Think about how '..' moves up one folder in a path.
✗ Incorrect
The relative URL '../image.png' moves one directory up from 'folder/', so the resolved URL is 'https://example.com/image.png'.
❓ Predict Output
intermediate2:00remaining
What happens when resolving an absolute URL against a base URL?
What will this Node.js code output?
Node.js
const { URL } = require('url');
const base = new URL('https://example.com/folder/page.html');
const absolute = new URL('https://other.com/path', base);
console.log(absolute.href);Attempts:
2 left
💡 Hint
Absolute URLs ignore the base URL.
✗ Incorrect
When the URL constructor receives an absolute URL as the first argument, it ignores the base URL and uses the absolute URL as is.
❓ component_behavior
advanced2:30remaining
How does URL resolution handle trailing slashes in base URLs?
Given this code, what will be the output?
Node.js
const { URL } = require('url');
const base1 = new URL('https://example.com/folder/');
const base2 = new URL('https://example.com/folder');
const relative = 'page.html';
console.log(new URL(relative, base1).href);
console.log(new URL(relative, base2).href);Attempts:
2 left
💡 Hint
Trailing slash means the base is treated as a folder, no trailing slash means it's a file.
✗ Incorrect
When the base URL ends with a slash, the relative URL appends inside that folder. Without the slash, the last part is treated as a file and replaced by the relative URL.
📝 Syntax
advanced2:00remaining
Which option causes a SyntaxError when creating a URL?
Which of these URL constructor calls will cause a SyntaxError in Node.js?
Attempts:
2 left
💡 Hint
Ports must be numeric and valid.
✗ Incorrect
Port numbers must be numeric and within valid range. 'invalid' is not a number, so it causes a SyntaxError.
🔧 Debug
expert3:00remaining
Why does this URL resolution produce an unexpected result?
Examine this code snippet and choose the correct explanation for the output.
Node.js
const { URL } = require('url');
const base = new URL('https://example.com/folder/page.html');
const relative = new URL('subfolder/../image.png', base);
console.log(relative.href);Attempts:
2 left
💡 Hint
Think about how '..' works in paths and how URL resolution normalizes paths.
✗ Incorrect
The '..' moves up one directory from 'folder/', so 'subfolder/../image.png' resolves to 'folder/image.png'. The URL class normalizes the path automatically.