আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি সংখ্যা গ্রহণ করে এবং তা প্যালিন্ড্রোম কিনা তা নির্ধারণ করে।
প্যালিন্ড্রোম সংখ্যা: এমন সংখ্যা যা বাম থেকে ডান এবং ডান থেকে বাম পড়লে একই থাকে।
উদাহরণস্বরূপ:
- ৩৪৩ একটি প্যালিন্ড্রোম সংখ্যা
- ৬৭৮৯৮৭৬ একটি প্যালিন্ড্রোম সংখ্যা
- ৪৫৬৭৬৪ প্যালিন্ড্রোম সংখ্যা নয়
উদাহরণ
const num1 = 343;
const num2 = 6789876;
const num3 = 456764;
const isPalindrome = num => {
let length = Math.floor(Math.log(num) / Math.log(10) + 1);
while(length > 0) {
let last = Math.abs(num - Math.floor(num/10)*10);
let first = Math.floor(num / Math.pow(10, length - 1));
if(first != last){
return false;
};
num -= Math.pow(10, length - 1) * first;
num = Math.floor(num/10);
length -= 2;
};
return true;
};
console.log(isPalindrome(num1));
console.log(isPalindrome(num2));
console.log(isPalindrome(num3));
আউটপুট
কনসোলে আউটপুট হবে -
true true false