您可以在 repo Github 上找到这篇文章中的所有代码。
/** * @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} 字符串 * @return {字符串} */ 函数压缩(str){ 如果 (!str) { 返回 ””; } 让压缩=“”; 让计数 = 1; for (让 i = 1; i 1){ 压缩=计数; } 计数 = 1; } } 返回压缩; } // 使用示例 console.log(压缩(“a”)); // '一个' console.log(压缩(“aa”)); // 'a2' console.log(压缩(“aaa”)); // 'a3' console.log(压缩(“aaab”)); // 'a3b' console.log(压缩(“aaabb”)); // 'a3b2' console.log(压缩(“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} 字符串 * @returns {字符串} */ 函数解压缩(str){ 常量堆栈=[]; 让当前数量 = 0; 让当前Str =“”; for (str 的 const char) { if (char >= "0" && char 'ababab' console.log(解压缩(“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} 字符串 * @return {数字} */ 函数最长长度(str){ const strArr = str.split(" "); 令长度=0; for (让 i = 0; i 长度) { 长度 = strArr[i].length; } } 返回长度; } // 使用示例 console.log(longestLength("最长的单词是 thelongestword")); // => 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} 字符串 * @return {字符串[]} */ 函数最长的单词(str){ const strArr = str.split(" "); 常量结果 = []; 让最大值= 0; for (strArr 的 const char) { if (char.length > max) { 最大=字符长度; } } for (strArr 的 const char) { if (char.length === max) { 结果.push(char); } } 返回结果.join(""); } // 使用示例 console.log(longestWord("最长的单词是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} 字符串 * @returns {字符串|细绳[]} */ 函数计数(str){ 常量地图=新地图(); 常量结果 = []; for (str 的 const char) { map.set(char, (map.get(char) ?? 0) 1); } const max = Math.max(...map.values()); for (map 的 const [键, 值]) { 如果(值===最大值){ 结果.push(key); } } 返回结果.length === 1 ?结果[0]:结果; } // 使用示例 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} 字符串 * @return {数字} */ 函数 getStringLength(str) { return Array.from(new Intl.Segmenter().segment(str)).length; } // 使用示例 console.log(getStringLength("测试?")); // => 5 console.log("测试?".length); // => 6
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3