Skip to content

Instantly share code, notes, and snippets.

@MikeCheng1208
Last active January 21, 2019 10:45
Show Gist options
  • Save MikeCheng1208/808a48bb832ccb92df508557d4ae6fe4 to your computer and use it in GitHub Desktop.
Save MikeCheng1208/808a48bb832ccb92df508557d4ae6fe4 to your computer and use it in GitHub Desktop.
註解版本
const toLowerCase = function(str) {
let strarr = str.split(""); // 先把傳入的字串變成陣列
let samtLetterArr = []; // 轉換後的陣列
let samtLetter = ""; // 最後回傳的字串
// 跑回圈去轉換
strarr.forEach(el => {
let unicode = el.charCodeAt(0); // 一個個把字串轉成 Unicode
let letter = String.fromCharCode(unicode); // 把 Unicode 轉回來字串,塞入 letter
if(unicode <= 90 && unicode >=65){ // 但是如果 Unicode 是需要轉換的話
letter = String.fromCharCode(unicode + 32); // 就把每個 Unicode 加 32 的間距,再塞回 letter
}
samtLetterArr.push(letter); // 轉換後的字串塞回轉換後的陣列中
});
samtLetter = samtLetterArr.join(''); // 再將陣列給合併成字串,回塞給 samtLetter
return samtLetter; // 回傳最後的字串
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment