Skip to content

Instantly share code, notes, and snippets.

@manzaloros
Last active November 8, 2022 16:05
Show Gist options
  • Save manzaloros/f2316c6431df1dc9818f587d30f33ed4 to your computer and use it in GitHub Desktop.
Save manzaloros/f2316c6431df1dc9818f587d30f33ed4 to your computer and use it in GitHub Desktop.
String Stuff
const replace = (word, index, replacement) => {
return word.substr(0, index) + replacement + word.substr(index + replacement.length);
}
// another replace:
const replaceChar = (index, newChar, string) => {
if (index < string.length) return `${string.substring(0, index)}${newChar}${string.substring(index + 1)}`
}
const differByCase = (c1, c2) => Math.abs(c1.charCodeAt(0) - c2.charCodeAt(0) === 32)
// makes a number from a number string, e.g. '100'
const makeNum = (string) => {
let num = 0;
for (let i = 0; i < string.length; i += 1) {
const digit = string[i];
if (isNaN(digit)) throw new Error('not a num!');
num = +digit + (num * 10);
}
return num;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment