### [ROT13](https://www.codewars.com/kata/52223df9e8f98c7aa7000062)**** - 知識點:String.fromCharCode()、正則、字串比大小 :::spoiler 解法 ``` javascript= function rot13(str) { const arr = str.split(""); const result = []; arr.forEach((e) => { const upperLetter = e.toUpperCase().charCodeAt(0); upperLetter >= 65 && upperLetter <= 77 ? result.push(String.fromCharCode(e.charCodeAt(0) + 13)) : upperLetter >= 78 && upperLetter <= 90 ? result.push(String.fromCharCode(e.charCodeAt(0) - 13)) : result.push(e); }); return result.join(""); } console.log(rot13("EBG13 rknzcyr.")); ``` - 字串可以直接來比大小,會自動用 ASCII 去判斷大小 ```javascript= console.log("a" <= "m"); // true console.log("r" <= "m"); // false console.log("你好" <= "哈囉"); // true ``` - `/[a-z]/ig` i 表示不分區分大小寫 ```javascript= function rot13(str) { return str.replace(/[a-z]/ig, function(x){ return String.fromCharCode(x.charCodeAt(0) + (x.toLowerCase() <= 'm' ? 13: -13)); }); } ``` ::: ### [The Hashtag Generator](https://www.codewars.com/kata/52449b062fb80683ec000024)**** - 知識點:replaceAll()+regex、every() :::spoiler 解法 ``` javascript= function generateHashtag(str) { if (!str || str.split("").every((e) => e == " ")) return false; const result = `#${str .replaceAll(/(^[a-z]|\s[a-z])/g, (match) => match.toUpperCase()) .replaceAll(" ", "")}`; return result.length <= 140 ? result : false; } console.log(generateHashtag(" ".repeat(200))); ``` ::: ### [Scramblies](https://www.codewars.com/kata/55c04b4cc56a697bb0000048)**** - 知識點: :::spoiler 解法 ``` javascript= function scramble(str1, str2) { const letter = str1.split("").reduce((obj, c) => { obj[c] ? obj[c]++ : (obj[c] = 1); return obj; }, {}); let result = true; for (let i = 0; i < str2.length; i++) { if (letter[str2[i]]) { letter[str2[i]]--; } else { result = false; } } return result; } scramble("cedewaraaossoqqyt", "codewars"); ``` ```javascript= function scramble(str1, str2) { let occurences = str1.split("").reduce((arr, cur) => { arr[cur] ? arr[cur]++ : arr[cur] = 1; return arr; }, {}); return str2.split("").every((character) => --occurences[character] >= 0); } ``` ::: ### [title]()** - 知識點: :::spoiler 解法 ``` javascript= ``` :::
{"title":"Codewars 5kyu","description":"知識點:","contributors":"[{\"id\":\"65ef09e4-76a5-4ffa-8afd-4e2465c6240e\",\"add\":2262,\"del\":11}]"}
Expand menu