ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript convertToLower() ที่ใช้วิธีการสตริงที่แปลงสตริงที่เรียกใช้เป็นสตริงตัวพิมพ์เล็กและส่งคืนสตริงใหม่
ตัวอย่างเช่น หากอินพุตของฟังก์ชันคือ
ป้อนข้อมูล
const str = 'ABcD123';
ผลผลิต
const output = 'abcd123';
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'ABcD123';
String.prototype.convertToLower = function(){
let res = '';
for(let i = 0; i < this.length; i++){
const el = this[i];
const code = el.charCodeAt(0);
if(code >= 65 && code <= 90){
res += String.fromCharCode(code + 32);
}else{
res += el;
};
};
return res;
};
console.log(str.convertToLower()); ผลลัพธ์
abcd123