ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่อยู่บนวัตถุต้นแบบของคลาสสตริง
ฟังก์ชันนี้ควรเปลี่ยนตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ของตัวอักษรทั้งหมดในสตริงเป็นตัวพิมพ์ใหญ่และส่งคืนสตริงใหม่
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'This is a lowercase String';
String.prototype.customToUpperCase = function(){
const legend = 'abcdefghijklmnopqrstuvwxyz';
const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let res = '';
for(let i = 0; i < this.length; i++){
const el = this[i];
const index = legend.indexOf(el);
if(index !== -1){
res += UPPER[index];
}else{
res += el;
};
};
return res;
};
console.log(str.customToUpperCase()); ผลลัพธ์
ต่อไปนี้เป็นเอาต์พุตคอนโซล -
THIS IS A LOWERCASE STRING