हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो दो स्ट्रिंग्स लेता है, मान लीजिए string1 और string2, और निर्धारित करता है कि string1, string2 से खत्म होती है या नहीं।
उदाहरण के लिए -
"The game is on" यहां, "on" को true लौटाना चाहिए
जबकि,
"the game is off" ऊपर, "of" को false लौटाना चाहिए
आइए इस फ़ंक्शन के लिए कोड लिखते हैं।
उदाहरण
const first = 'The game is on';
const second = ' on';
const endsWith = (first, second) => {
const { length } = second;
const { length: l } = first;
const sub = first.substr(l - length, length);
return sub === second;
};
console.log(endsWith(first, second));
console.log(endsWith(first, ' off'));आउटपुट
कंसोल में आउटपुट होगा -
true false