문제
우리는 두 개의 JavaScript 함수를 작성해야 합니다 -
- 첫 번째 함수는 긴 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