Palindrome Number is the question from leetcode with the difficulty level of easy and asked in many interviews including FAANG companies.  


The question for an algorithm is, 


Given an integer x, return true if x is a palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

For example, 121 is a palindrome while 123 is not.


Example 1:

Input: x = 121

Output: true

Explanation: 121 reads as 121 from left to right and from right to left.


Example 2:

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.


Example 3:

Input: x = 10

Output: false

Explanation: Reads 01 from right to left. Therefore it is not a palindrome.


Follow up: Could you solve it without converting the integer to a string?


Let's start with the algorithm,

We will be going to check the number then we will reverse that number and match that with the original number. Simple!


Here is the algorithm,

var isPalindrome = function(x) {

    var reverse = 0;

    var copy = x;


    while (copy > 0) {

      const digit = copy % 10;

      reverse = reverse * 10 + digit;

      copy = Math.floor(copy / 10); //Math.floor(copy / 10)

    }


    return reverse == x;

};


Here we are storing the x into the variable copy and using a while loop till the copy is less than zero. In the loop, We are going to use a reverse variable in which we are storing the reverse of the number and in the final statement of return, we are comparing reverse with 'x', which will return true or false. 


I hope you understood the algorithm, if you have any doubt please let us know in comments.