เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสองสตริงและตรวจสอบว่าสตริงแรกเริ่มต้นด้วยวินาทีหรือไม่
ตัวอย่างเช่น −
If the two strings are: “Disaster management report” “Disas” Then our function should return true
มาเขียนโค้ดสำหรับฟังก์ชันนี้กัน −
ตัวอย่าง
const first = 'The game is on';
const second = 'The';
const startsWith = (first, second) => {
const { length } = second;
const { length: l } = first;
const sub = first.substr(0, length);
return sub === second;
};
console.log(startsWith(first, second)); ผลลัพธ์
ผลลัพธ์ในคอนโซลจะเป็น -
true