Sie können den gesamten Code in diesem Beitrag im Repo Github finden.
/** * @param {any} char * @return {Boolean} */ function isAlphaNumeric(char) { return /[A-Za-z0-9]/.test(char); } // Usage example console.log(isAlphaNumeric("a")); // => true console.log(isAlphaNumeric(0)); // => true console.log(isAlphaNumeric("!")); // => false
// camel case to snake case /** * @param {string} str * @return {string} */ function toKebabCase(str) { let temp = str.replace(/[A-Z]/g, function (i) { return "_" i.toLowerCase(); }); if (temp[0] === "_") { temp = temp.slice(1); } return temp; } // Usage example console.log(toKebabCase("testMethod")); // => "test_method" // snake case to camel case /** * @param {string} str * @return {string} */ function toCamelCase(str) { return str.replace( /([a-z])_([a-z])/gi, (_, left, right) => left right.toUpperCase() ); } // Usage example console.log(toCamelCase("test_method")); // => "testMethod"
/** * @param {string} version1 * @param {string} version2 * @return {number} */ function compareVersion(version1, version2) { const v1Arr = version1.split("."); const v2Arr = version2.split("."); for (let i = 0; i v2Num) { return 1; } else if (v1Num 1, meaning first one is greater console.log(compareVersion("12.1.0", "12.1.2")); // => -1, meaning latter one is greater console.log(compareVersion("5.0.1", "5.0.1")); // => 0, meaning they are equal.
/** * @param {string[]} versions * @return {string[]} */ function sortVersions(versions) { return versions.sort((a, b) => { const aParts = a.split(".").map(Number); const bParts = b.split(".").map(Number); for (let i = 0; i [ '0.1.1', '0.302.1', '2.3.3', '4.2', '4.3.4.5', '4.3.5' ]
/** * @param {string} str * @return {string} */ function compress(str) { if (!str) { return ""; } let compressed = ""; let count = 1; for (let i = 1; i 1) { compressed = count; } count = 1; } } return compressed; } // Usage example console.log(compress("a")); // 'a' console.log(compress("aa")); // 'a2' console.log(compress("aaa")); // 'a3' console.log(compress("aaab")); // 'a3b' console.log(compress("aaabb")); // 'a3b2' console.log(compress("aaabba")); // 'a3b2a'/** * @param {string} str * @return {string} */ Funktion compress(str) { if (!str) { zurückkehren ""; } let compress = ""; lass zählen = 1; for (let i = 1; i 1) { komprimiert = zählen; } Anzahl = 1; } } komprimiert zurückkehren; } // Anwendungsbeispiel console.log(compress("a")); // 'A' console.log(compress("aa")); // 'a2' console.log(compress("aaa")); // 'a3' console.log(compress("aaab")); // 'a3b' console.log(compress("aaabb")); // 'a3b2' console.log(compress("aaabba")); // 'a3b2a'
/** * @param {string} str * @return {string} */ function compress(str) { if (!str) { return ""; } let compressed = ""; let count = 1; for (let i = 1; i 1) { compressed = count; } count = 1; } } return compressed; } // Usage example console.log(compress("a")); // 'a' console.log(compress("aa")); // 'a2' console.log(compress("aaa")); // 'a3' console.log(compress("aaab")); // 'a3b' console.log(compress("aaabb")); // 'a3b2' console.log(compress("aaabba")); // 'a3b2a'/** * @param {string} str * @returns {string} */ Funktion uncompress(str) { const stack = []; let currentNum = 0; let currentStr = ""; for (const char of str) { if (char >= "0" && char 'ababab' console.log(uncompress("3(ab2(c))")); // => 'abccabccabcc'
/** * @param {string} str * @return {string} */ function compress(str) { if (!str) { return ""; } let compressed = ""; let count = 1; for (let i = 1; i 1) { compressed = count; } count = 1; } } return compressed; } // Usage example console.log(compress("a")); // 'a' console.log(compress("aa")); // 'a2' console.log(compress("aaa")); // 'a3' console.log(compress("aaab")); // 'a3b' console.log(compress("aaabb")); // 'a3b2' console.log(compress("aaabba")); // 'a3b2a'/** * @param {string} str * @return {Nummer} */ Funktion longestLength(str) { const strArr = str.split(" "); sei Länge = 0; for (let i = 0; i length) { length = strArr[i].length; } } Rückgabelänge; } // Anwendungsbeispiel console.log(longestLength("Das längste Wort ist das längste Wort")); // => 14
/** * @param {string} str * @return {string} */ function compress(str) { if (!str) { return ""; } let compressed = ""; let count = 1; for (let i = 1; i 1) { compressed = count; } count = 1; } } return compressed; } // Usage example console.log(compress("a")); // 'a' console.log(compress("aa")); // 'a2' console.log(compress("aaa")); // 'a3' console.log(compress("aaab")); // 'a3b' console.log(compress("aaabb")); // 'a3b2' console.log(compress("aaabba")); // 'a3b2a'/** * @param {string} str * @return {string[]} */ Funktion longestWord(str) { const strArr = str.split(" "); const result = []; sei max = 0; for (const char of strArr) { if (char.length > max) { max = char.length; } } for (const char of strArr) { if (char.length === max) { result.push(char); } } return result.join(""); } // Anwendungsbeispiel console.log(longestWord("Das längste Wort ist das längste Wort")); // => "thelongestword"
/** * @param {string} str * @return {string} */ function compress(str) { if (!str) { return ""; } let compressed = ""; let count = 1; for (let i = 1; i 1) { compressed = count; } count = 1; } } return compressed; } // Usage example console.log(compress("a")); // 'a' console.log(compress("aa")); // 'a2' console.log(compress("aaa")); // 'a3' console.log(compress("aaab")); // 'a3b' console.log(compress("aaabb")); // 'a3b2' console.log(compress("aaabba")); // 'a3b2a'/** * @param {string} str * @returns {string | string[]} */ Funktion count(str) { const map = new Map(); const result = []; for (const char of str) { map.set(char, (map.get(char) ?? 0) 1); } const max = Math.max(...map.values()); for (const [Schlüssel, Wert] der Karte) { if (Wert === max) { result.push(key); } } return result.length === 1 ? Ergebnis[0]: Ergebnis; } // Anwendungsbeispiel console.log(count("abbccc")); // => 'c' console.log(count("abbcccddd")); // => ['c', 'd'];
/** * @param {string} str * @return {string} */ function compress(str) { if (!str) { return ""; } let compressed = ""; let count = 1; for (let i = 1; i 1) { compressed = count; } count = 1; } } return compressed; } // Usage example console.log(compress("a")); // 'a' console.log(compress("aa")); // 'a2' console.log(compress("aaa")); // 'a3' console.log(compress("aaab")); // 'a3b' console.log(compress("aaabb")); // 'a3b2' console.log(compress("aaabba")); // 'a3b2a'/** * @param {String} str * @return {Number} */ Funktion getStringLength(str) { return Array.from(new Intl.Segmenter().segment(str)).length; } // Anwendungsbeispiel console.log(getStringLength("test?")); // => 5 console.log("test?".length); // => 6
Haftungsausschluss: Alle bereitgestellten Ressourcen stammen teilweise aus dem Internet. Wenn eine Verletzung Ihres Urheberrechts oder anderer Rechte und Interessen vorliegt, erläutern Sie bitte die detaillierten Gründe und legen Sie einen Nachweis des Urheberrechts oder Ihrer Rechte und Interessen vor und senden Sie ihn dann an die E-Mail-Adresse: [email protected] Wir werden die Angelegenheit so schnell wie möglich für Sie erledigen.
Copyright© 2022 湘ICP备2022001581号-3