เราต้องเขียนฟังก์ชันตรวจสอบว่าสตริงจบด้วยตัวอักษรที่กำหนดหรือไม่ โดยห้ามใช้เมธอด ES6 หรือไลบรารี
ตัวอย่างโค้ด
const str = 'This is a string';
const checkEnding = (str = '', char = '') => {
const getLast = (str = '') => {
const { length } = str;
return str[length - 1];
};
return getLast(str) === char;
};
console.log(checkEnding(str, 'g')); // true
console.log(checkEnding(str, 'h')); // false
ผลลัพธ์
ค่าที่แสดงในคอนโซลจะเป็นดังนี้:
true false