Determine whether an integer is a palindrome The task


  Determine whether an integer is a palindrome. An integer is a
  palindrome when it reads the same backward as forward.
  
  Example 1:
  
  Input: 121 Output: true 
  
  Example 2:
  
  Input: -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: 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?


My solution
with converting number to string

const isPalindrome = n =&gt; n &gt;= 0 &amp;&amp; Number([...`${n}`].reverse().join("")) === n;

console.log(isPalindrome(121));


My solution
without converting number to string

const isPalindrome2 = n =&gt; {
  if (n &lt; 0) { return false; }
  let num = Math.abs(n);
  const arr = [];
  let i = 1;
  while (num &gt; 0) {
    const min = num % (10 ** i);
    num = num - min;
    i++;
    arr.push(min);
  }
  i = i - 2;
  let j = 0;
  return n === arr.reduce((res, x) =&gt; {
    const add = (x/ (10 ** j)) * (10 ** i);
    res += add;
    i--;
    j++;
    return res;
  }, 0);
};

console.log(isPalindrome2(121));
Your second solution is awfully complicated for something simple.

You start out alright.  No negative number can be a palindrome, so we can discount those. However, 0 is always a palindrome, so you're discounting that while you shouldn't.

You do a lot of complicated math and use an array. You could use either, but shouldn't use both. There are a couple of mathematical approaches to solve this, but you should be able to use those without iterating over the individual numbers or splitting it up at all. The most obvious solution however, is using an array and direct comparisons, without any math.

Say we got 1221. Split it up. [1, 2, 2, 1]. Iterate over the array, comparing every nth character to the last-nth character. 0th to 3rd. 1st to 2nd.

Say we got 92429. Split it up. [9, 2, 4, 2, 9]. Ignore the middle character. Handle the rest like it's an even-length number.

Based on those 2 cases, you should be able to figure out a much simpler algorithm.

Note: This answers the explicit question. Implicitly, you should wonder whether arrays should be allowed for this challenge. After all, iterating over a string or an array, it's not that different. I strongly suspect they want you to use the math-only approach.

Another approach, which is somewhat math-based and you should beware of overflows, is simply reversing the number.
In pseudo-code, that would look something like this:

reverse = 0
while (number != 0) {
  reverse = reverse * 10 + number % 10;
  number /= 10;
}


Check the input versus its reversed number. If they are the same, it's a palindrome.

But this still uses extra memory to hold the additional integer we just created.

Can it be done without? Absolutely. But I'll leave that as an exercise for you.