Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. const isPalindrome = n => {
  2. if (n < 0) { return false; }
  3. let num = Math.abs(n);
  4. const arr = [];
  5. let i = 1;
  6. while (num > 0) {
  7. const min = num % (10 ** i);
  8. num = num - min;
  9. i++;
  10. arr.push(min);
  11. }
  12. i = i - 2;
  13. let j = 0;
  14. return n === arr.reduce((res, x) => {
  15. const add = (x/ (10 ** j)) * (10 ** i);
  16. res += add;
  17. i--;
  18. j++;
  19. return res;
  20. }, 0);
  21. };
  22.  
  23. console.log(isPalindrome(121));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement