// Object to store URL mappings urlDatabase[shortCode] = longURL
const urlDatabase = {};
// Function to generate a random short code
function generateShortCode() {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let shortCode = '';
for (let i = 0; i < 8; i++) {
shortCode += characters.charAt(Math.floor(Math.random() * characters.length));
}
return shortCode;
}
// Function to shorten a URL
function shortenURL(longURL) {
const shortCode = generateShortCode();
urlDatabase[shortCode] = longURL;
return `https://short.url/${shortCode}`;
}
// Function to retrieve the long URL
function getLongURL(shortURL) {
const shortCode = shortURL.split('/').pop();
return urlDatabase[shortCode] || 'URL not found!';
}
const longURL = 'https://www.seek4info.com/search.php?query=printf';
const shortURL = shortenURL(longURL);
console.log('Shortened URL:', shortURL);
const retrievedLongURL = getLongURL(shortURL);
console.log('Retrieved URL:', retrievedLongURL);
/*
run:
Shortened URL: https://short.url/2V6zLvhz
Retrieved URL: https://www.seek4info.com/search.php?query=printf
*/