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