Goal: create a function
getUrlParams(url)that takes a URL string and returns an object containing all its GET parameters as key-value pairs.
%20 should become a space).function getUrlParams(url) {
// Create a URL instance to easily access searchParams
const searchParams = new URL(url).searchParams;
// Convert the URLSearchParams iterator to an object
return Object.fromEntries(searchParams.entries());
}Why this way:
function getUrlParams(url) {
const params = {};
const urlParts = url.split('?');
if (urlParts.length > 1) {
const queryString = urlParts[1];
const pairs = queryString.split('&');
for (const pair of pairs) {
// Handle cases where a key exists without a value (e.g., ?a&b=2)
const [key, value] = pair.split('=');
if (key) {
// decodeURIComponent to decode, e.g., %20 into a space
params[decodeURIComponent(key)] = decodeURIComponent(value || '');
}
}
}
return params;
}Why this way:
Write a function getUrlParams that takes a URL string as input and returns an object where the keys are the names of the GET parameters and the values are their values.
getUrlParams('https://example.com?a=1&b=hello'); // { a: '1', b: 'hello' }
getUrlParams('https://example.com?search=what%20is%20this'); // { search: 'what is this' }
getUrlParams('https://example.com'); // {}getUrlParams.%20 should become a space).Goal: create a function
getUrlParams(url)that takes a URL string and returns an object containing all its GET parameters as key-value pairs.
%20 should become a space).function getUrlParams(url) {
// Create a URL instance to easily access searchParams
const searchParams = new URL(url).searchParams;
// Convert the URLSearchParams iterator to an object
return Object.fromEntries(searchParams.entries());
}Why this way:
function getUrlParams(url) {
const params = {};
const urlParts = url.split('?');
if (urlParts.length > 1) {
const queryString = urlParts[1];
const pairs = queryString.split('&');
for (const pair of pairs) {
// Handle cases where a key exists without a value (e.g., ?a&b=2)
const [key, value] = pair.split('=');
if (key) {
// decodeURIComponent to decode, e.g., %20 into a space
params[decodeURIComponent(key)] = decodeURIComponent(value || '');
}
}
}
return params;
}Why this way:
Write a function getUrlParams that takes a URL string as input and returns an object where the keys are the names of the GET parameters and the values are their values.
getUrlParams('https://example.com?a=1&b=hello'); // { a: '1', b: 'hello' }
getUrlParams('https://example.com?search=what%20is%20this'); // { search: 'what is this' }
getUrlParams('https://example.com'); // {}getUrlParams.%20 should become a space).The code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!