0
0
Node.jsframework~20 mins

Encoding and decoding URLs in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Encoding 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 decoding a URL component?
Consider the following Node.js code snippet that decodes a URL component. What will be the output?
Node.js
const encoded = 'Hello%20World%21';
const decoded = decodeURIComponent(encoded);
console.log(decoded);
AHello World!
BHello%20World%21
CHello+World!
DSyntaxError
Attempts:
2 left
💡 Hint
decodeURIComponent converts percent-encoded characters back to normal characters.
Predict Output
intermediate
2:00remaining
What does encodeURI do with special characters?
What is the output of this Node.js code that uses encodeURI on a URL string?
Node.js
const url = 'https://example.com/a file.html?name=John Doe&age=30';
const encoded = encodeURI(url);
console.log(encoded);
Ahttps%3A%2F%2Fexample.com%2Fa%20file.html%3Fname%3DJohn%20Doe%26age%3D30
Bhttps://example.com/a%20file.html%3Fname%3DJohn%20Doe%26age%3D30
Chttps://example.com/a%20file.html?name=John%20Doe&age=30
Dhttps://example.com/a file.html?name=John Doe&age=30
Attempts:
2 left
💡 Hint
encodeURI encodes spaces but leaves URL special characters like ':' and '?' intact.
📝 Syntax
advanced
2:00remaining
Which option causes a SyntaxError when decoding a URL component?
Which of the following decodeURIComponent calls will cause a SyntaxError in Node.js?
AdecodeURIComponent('%E0%A4%A');
BdecodeURIComponent('%20');
CdecodeURIComponent('%3F');
DdecodeURIComponent('Hello%20World');
Attempts:
2 left
💡 Hint
Invalid percent-encoding sequences cause SyntaxError in decodeURIComponent.
component_behavior
advanced
2:00remaining
How does encodeURIComponent treat reserved URL characters?
Given the string 'name=John&age=30', what will encodeURIComponent produce?
Node.js
const input = 'name=John&age=30';
const output = encodeURIComponent(input);
console.log(output);
Aname%3DJohn%26age=30
Bname=John&age=30
Cname%3DJohn&age%3D30
Dname%3DJohn%26age%3D30
Attempts:
2 left
💡 Hint
encodeURIComponent encodes all characters except letters, digits, and a few others.
state_output
expert
2:00remaining
What is the final value of 'url' after encoding and decoding?
Consider this Node.js code. What is the final value of the variable 'url' after running all lines?
Node.js
let url = 'https://example.com/search?query=Node.js & encoding';
url = encodeURI(url);
url = decodeURIComponent(url);
Ahttps://example.com/search?query=Node.js%20&%20encoding
Bhttps://example.com/search?query=Node.js & encoding
Chttps://example.com/search?query=Node.js%20%26%20encoding
Dgnidocne & sj.edoN=yreuq?hcraes/moc.elpmaxe//:sptth
Attempts:
2 left
💡 Hint
encodeURI encodes spaces as '%20' but leaves '&' intact; decodeURIComponent reverses encoding.