您可以在 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