0
0
Node.jsframework~20 mins

URLSearchParams for query strings in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URLSearchParams Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this code using URLSearchParams?
Consider the following Node.js code snippet that uses URLSearchParams. What will be printed to the console?
Node.js
const params = new URLSearchParams('name=alice&age=30&city=NY');
console.log(params.get('age'));
A"NY"
B"alice"
Cnull
D"30"
Attempts:
2 left
💡 Hint
Remember that get returns the value of the given key from the query string.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a URLSearchParams object from an object?
You want to create a URLSearchParams instance from this object: { foo: 'bar', count: 5 }. Which code snippet works without error?
Anew URLSearchParams([['foo', 'bar'], ['count', '5']])
Bnew URLSearchParams({ foo: 'bar', count: 5 })
Cnew URLSearchParams('foo=bar&count=5')
Dnew URLSearchParams({ foo: 'bar', count: '5' })
Attempts:
2 left
💡 Hint
URLSearchParams accepts a string or an iterable of key-value pairs, not a plain object.
🔧 Debug
advanced
2:00remaining
Why does this code throw a TypeError?
Examine the code below. Why does it throw a TypeError?
Node.js
const params = new URLSearchParams({ key: 'value' });
console.log(params.toString());
AURLSearchParams constructor does not accept plain objects, only strings or iterable key-value pairs
BThe toString method is not defined on URLSearchParams instances
CThe key 'key' is reserved and cannot be used in URLSearchParams
DThe value 'value' must be a number, not a string
Attempts:
2 left
💡 Hint
Check the allowed argument types for URLSearchParams constructor.
state_output
advanced
2:00remaining
What is the final query string after these operations?
Given the following code, what will params.toString() output?
Node.js
const params = new URLSearchParams('a=1&b=2&c=3');
params.set('b', '20');
params.append('d', '4');
params.delete('a');
A"a=1&b=20&c=3"
B"b=20&c=3&d=4"
C"b=2&c=3&d=4"
D"a=1&b=20&c=3&d=4"
Attempts:
2 left
💡 Hint
Remember that set replaces the value, append adds a new value, and delete removes the key.
🧠 Conceptual
expert
3:00remaining
Which option correctly explains how URLSearchParams handles multiple values for the same key?
If a query string contains multiple values for the same key, how does URLSearchParams behave when using get and getAll methods?
A<code>get</code> returns the last value; <code>getAll</code> returns only the first value
B<code>get</code> returns all values as a comma-separated string; <code>getAll</code> returns the last value
C<code>get</code> returns the first value; <code>getAll</code> returns all values as an array
D<code>get</code> throws an error if multiple values exist; <code>getAll</code> returns all values
Attempts:
2 left
💡 Hint
Think about how to retrieve one or all values for a repeated key.