Question:
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example:
Input: "Hello"
Output: "hello"
Input: "here"
Output: "here"
Input: "LOVELY"
Output: "lovely"
Solution:
/**
* @param {string} str
* @return {string}
*/
var toLowerCase = function(str) {
// ASCII 编码大写小写相差32
let arr = str.split('');
let AscCode;
let maxCode = 'Z'.charCodeAt();
let minCode = 'A'.charCodeAt();
for (let i = 0; i < arr.length; i++) {
// 转换为ASCII码
AscCode = arr[i].charCodeAt();
// 大写字母,转小写
if (maxCode >= AscCode && minCode <= AscCode) {
arr[i] = String.fromCharCode(AscCode+32);
}
}
return arr.join('');
};
Runtime: 48 ms, faster than 100.00% of JavaScript online submissions for To Lower Case.