Skip to content

Instantly share code, notes, and snippets.

@andycmaj
Last active May 5, 2022 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andycmaj/7e540f047546bf61a4c78b245f4e9545 to your computer and use it in GitHub Desktop.
Save andycmaj/7e540f047546bf61a4c78b245f4e9545 to your computer and use it in GitHub Desktop.
export const problem1 = function (length = 12) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
export const problem2 = (str: string) => {
return str.replace(/[^a-zA-Z0-9-]/g, "-").toLowerCase();
};
export const prob3_a = (e) => {
return e.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
};
export const prob3_b = (e) => {
return e.match(/([A-Z])/g).reduce(
(str, c) => str.replace(new RegExp(c), '_' + c.toLowerCase()),
e
)
.substring((e.slice(0, 1).match(/([A-Z])/g)) ? 1 : 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment