while I try my solution for the given example it fails at the leetcode although it provide the expected output at my VScode
Given an integer x, return true if x is a palindrome and false otherwise.
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
my answer
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (Math.sign(x) === -1) {
console.log("false")
return "false";
} else {
let y = x.toString(); // convert the number to string
let strArray = y.split('') // convert the string to array of strings
let reversed = strArray.reverse() // reverse the array
let convString = reversed.join('') // convert the array to string
let revX = parseInt(convString)
if (x === revX) {
return "true"
} else {
return "false"
}
}
};
the leetcode test didn`t pass my solution while it pass when i test it myself
console.log(strArray);,console.log(reversed);, etc., so you see where it goes wrong.