আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা দুটি স্ট্রিং, বলুন, স্ট্রিং1 এবং স্ট্রিং2 নেয় এবং স্ট্রিং1 স্ট্রিং2 দিয়ে শেষ হয় কিনা তা নির্ধারণ করে।
যেমন −
"The game is on" Here, "on" should return true
যখন,
"the game is off" Above, “of" should return 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