เราจำเป็นต้องเขียนฟังก์ชัน JavaScript String ที่เขียนทับค่าเริ่มต้น toLowerCase() และควรมีฟังก์ชันเดียวกันกับฟังก์ชันเริ่มต้น
มาเขียนโค้ดสำหรับฟังก์ชันนี้กัน −
ตัวอย่าง
const str = 'Some UpPerCAsE LeTTeRs!!!';
const toLowerCase = function(){
let str = '';
for(let i = 0; i < this.length; i++){
const ascii = this[i].charCodeAt();
if(ascii >= 65 && ascii <= 90){
str += String.fromCharCode(ascii + 32);
}else{
str += this[i];
};
};
return str;
};
String.prototype.toLowerCase = toLowerCase;
console.log(str.toLowerCase()); ผลลัพธ์
ผลลัพธ์ในคอนโซลจะเป็น -
some uppercase letters!!!