समस्या
हमें दो जावास्क्रिप्ट फ़ंक्शन लिखने की आवश्यकता है -
- पहले फ़ंक्शन को एक लंबा यूआरएल लेना चाहिए और एक छोटा यूआरएल देना चाहिए जो उससे मेल खाता हो।
- दूसरा कार्य संक्षिप्त यूआरएल लेना चाहिए और मूल यूआरएल पर रीडायरेक्ट करना चाहिए।
उदाहरण
निम्नलिखित कोड है -
const url = 'https://www.google.com/search?client=firefox-b-d&q=google+search';
const obj = {};
const urlShortener = (longURL = '') => {
let shortURL = "short.ly/" + longURL.replace(/[^a-z]/g,'').slice(-4);
if(!obj[shortURL]){
obj[shortURL] = longURL;
};
return shortURL;
}
const urlRedirector = (shortURL = '') => {
return obj[shortURL];
};
const short = urlShortener(url);
const original = urlRedirector(short);
console.log(short);
console.log(original); आउटपुट
कंसोल आउटपुट निम्नलिखित है -
short.ly/arch https://www.google.com/search?client=firefox-b-d&q=google+search