ปัญหา
เราต้องเขียนฟังก์ชัน 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