forked from phongvdoan/seattle-301d60
-
Notifications
You must be signed in to change notification settings - Fork 0
Read: 09
Dayne edited this page Dec 12, 2019
·
1 revision
const URLstore = [];
function makeShort(URL) {
const rndName = Math.random().toString(36).substring(2);
URLstore.push({[rndName]: URL});
return rndName;
}
function getLong(shortURL) {
for (let i = 0; i < URLstore.length; i++) {
if (URLstore[i].hasOwnProperty(shortURL) !== false) {
return URLstore[i][shortURL];
}
}
}
const URLstore = new Map(); // Change this to a Map
function makeShort(URL) {
const rndName = Math.random().toString(36).substring(2); // Place the short URL into the Map as the key with the long URL as the value
URLstore.set(rndName, URL);
return rndName;
}
function getLong(shortURL) { // Leave the function early to avoid an unnecessary else statement
if (URLstore.has(shortURL) === false) {
throw 'Not in URLstore!';
}
return URLstore.get(shortURL); // Get the long URL out of the Map
}
Definition: It returns the same result if given the same arguments (it is also referred as deterministic). It does not cause any observable side effects (Examples of observable side effects include modifying a global object or a parameter passed by reference.).