0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query String Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'));
A"alice" "30"
B"name" "age"
Cundefined undefined
DSyntaxError
Attempts:
2 left
💡 Hint
Think about how URLSearchParams extracts values by key.
Predict Output
intermediate
2: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'));
A"green"
B"red,blue,green"
C"red"
DTypeError
Attempts:
2 left
💡 Hint
URLSearchParams.get returns the first value for the key.
🔧 Debug
advanced
2: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'));
ABecause URLSearchParams requires all keys to have values, missing '=' causes TypeError.
BBecause 'baz' is an invalid key and causes SyntaxError.
CBecause get() method is not defined on URLSearchParams.
DBecause 'baz' has no '=' sign and no value, get('baz') returns an empty string, not a TypeError.
Attempts:
2 left
💡 Hint
Check how URLSearchParams treats keys without values.
component_behavior
advanced
2: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());
A"key=value2"
B"key=value1&key=value2"
C"key=value1,value2"
D"key=value1"
Attempts:
2 left
💡 Hint
append() adds a new value for the same key without replacing existing ones.
📝 Syntax
expert
3: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.
Aconst params = new URLSearchParams('a=1&b=2'); params.append('c', 3); console.log(params.toString());
Bconst params = new URLSearchParams('a=1&b=2'); params.add('c', 3); console.log(params.toString());
Cconst params = new URLSearchParams('a=1&b=2'); params.push('c', 3); console.log(params.toString());
Dconst params = new URLSearchParams('a=1&b=2'); params.set('c', 3); console.log(params.toString());
Attempts:
2 left
💡 Hint
Check the correct method name to add a new key-value pair without replacing existing keys.