-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortener.js
33 lines (31 loc) · 1.3 KB
/
shortener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var alphabet = "abcdefghijklmnopqrstuvwxyz-0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var base = alphabet.length;
console.log("base: ", base);
var shortUrl = [];
module.exports = {
//Genearte shortened URL using base 10 to base64 conversion
getShortUrl: function(inputUrlCode) {
while (inputUrlCode > 0) {
var quotient = Math.floor(inputUrlCode / base);
var remainder = Math.floor(inputUrlCode % base);
inputUrlCode = quotient;
shortUrl.push(remainder);
}
console.log("shortUrl: ", shortUrl);
//console.log("reversed shortUrl: ", shortUrl.reverse());
//console.log("shortUrl: ", shortUrl);
var convertedBase = shortUrl.reverse();
console.log("convertedBase", convertedBase);
var resultArray = [];
for (i = 0; i < convertedBase.length; i++) {
resultArray[i] = alphabet[convertedBase[i]];
}
console.log("resultArray: ", resultArray.toString());
var shortenedUrl = resultArray.toString().replace(/\,/g, "");
console.log("Converted base shortened url:", shortenedUrl);
shortUrl.fill('');
console.log("shortUrl = []: ", shortUrl);
return shortenedUrl;
}
};
//what if earth is that heaven everybody is sent for good deeds in past lives