Here is the code which i tried to solve, for the leetcode problem in javaScript https://leetcode.com/problems/palindrome-number/ Problem i am facing:
- It is working for even number of digits in an intArr, but not for odd.
let myFunc = (num) => Number(num);
var intArr = Array.from(String(myInt), myFunc);
console.log(intArr.length);
const half = Math.ceil(intArr.length / 2);
const firstHalf = intArr.splice(0, half);
const secondHalf = intArr.splice(-half).reverse();
if (intArr.length % 2 != 0) {
firstHalf.pop();
}
console.log(firstHalf);
console.log(secondHalf);
arrayEquals(firstHalf, secondHalf);
function arrayEquals(firstHalf, secondHalf) {
if (
Array.isArray(firstHalf) &&
Array.isArray(secondHalf) &&
firstHalf.length === secondHalf.length &&
firstHalf.every((val, index) => val === secondHalf[index])
) {
console.log("true");
} else {
console.log("false");
}
}
}
isPalindrome(1221);
isPalindrome(12321);
Please let me know how can i solve this problem. Please correct me if I was wrong in implementing this solution, Thank you.