Challenge - 5 Problems
Query String Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of parsing a simple query string?
Given the following Node.js code using the built-in URLSearchParams, what will be logged to the console?
Node.js
const queryString = 'name=alice&age=30'; const params = new URLSearchParams(queryString); console.log(params.get('name'), params.get('age'));
Attempts:
2 left
💡 Hint
Think about how URLSearchParams extracts values by key.
✗ Incorrect
URLSearchParams parses the query string and allows access to values by their keys using get(). Here, 'name' returns 'alice' and 'age' returns '30'.
❓ Predict Output
intermediate2:00remaining
How does URLSearchParams handle repeated keys?
What will the following code output when parsing a query string with repeated keys?
Node.js
const queryString = 'color=red&color=blue&color=green'; const params = new URLSearchParams(queryString); console.log(params.get('color'));
Attempts:
2 left
💡 Hint
URLSearchParams.get returns the first value for the key.
✗ Incorrect
When keys repeat, get() returns the first value only. To get all values, use getAll().
🔧 Debug
advanced2:00remaining
Why does this code throw an error when parsing a query string?
Consider this code snippet. Why does it throw a TypeError?
Node.js
const queryString = 'foo=bar&baz'; const params = new URLSearchParams(queryString); console.log(params.get('baz'));
Attempts:
2 left
💡 Hint
Check how URLSearchParams treats keys without values.
✗ Incorrect
URLSearchParams treats keys without '=' as keys with empty string values. get('baz') returns an empty string, not an error.
❓ component_behavior
advanced2:00remaining
What is the behavior of URLSearchParams.append() with existing keys?
Given this code, what will be the output of params.toString()?
Node.js
const params = new URLSearchParams('key=value1'); params.append('key', 'value2'); console.log(params.toString());
Attempts:
2 left
💡 Hint
append() adds a new value for the same key without replacing existing ones.
✗ Incorrect
append() adds another value for the same key, so the query string has two key=value pairs.
📝 Syntax
expert3:00remaining
Which option correctly parses and modifies a query string to add a new parameter?
Select the code snippet that correctly parses 'a=1&b=2', adds 'c=3', and outputs the updated query string.
Attempts:
2 left
💡 Hint
Check the correct method name to add a new key-value pair without replacing existing keys.
✗ Incorrect
append() adds a new key-value pair. set() replaces existing key's value. add() and push() are not valid methods.