```javascript ``` ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200403222109559.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text... ...
<script>
let str = "Hello World!";
// 將字元串的字元全部轉換為小寫字元
function lowerCase(str) {
let arr = str.split("");
let newStr = "";
//通過for迴圈遍曆數組
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= 'A' && arr[i] <= 'Z')
newStr += arr[i].toLowerCase();
else
newStr += arr[i];
}
return newStr;
}
// 將字元串的字元全部轉換為大寫字元
function upperCase(str) {
let arr = str.split("");
let newStr = "";
// 通過數組的forEach方法來遍曆數組
arr.forEach(function (value) {
if (value >= 'a' && value <= 'z')
newStr += value.toUpperCase();
else
newStr += value;
});
return newStr;
}
let res1 = lowerCase(str);
let res2 = upperCase(str);
console.log(res1);
console.log(res2);
</script>