समस्या
हमें दो जावास्क्रिप्ट फ़ंक्शन लिखने हैं:
- पहला फ़ंक्शन एक लंबा URL लेगा और उसके अनुरूप एक छोटा URL लौटाएगा।
- दूसरा फ़ंक्शन छोटा URL लेगा और मूल URL पर रीडायरेक्ट करेगा।
उदाहरण
निम्न कोड देखें:
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