Created
January 14, 2025 19:09
-
-
Save onejohi/e46ddfdc03fedbfefeec6396fc3d1e1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Given an integer x, return true if x is a palindrome, and false otherwise. | |
| const isPalindrome = function(x) { | |
| if (x !== 0 && x % 10 === 0) { | |
| return false | |
| } | |
| const arrayX = Array.from(String(x), Number); | |
| const firstHalf = arrayX.slice(0, Math.round(arrayX.length / 2)) | |
| const lastHalf = arrayX.slice(Math.round(arrayX.length / 2) - 1, arrayX.length).reverse() | |
| for (let i = 0; i < firstHalf.length; i++) { | |
| if (firstHalf[i] !== lastHalf[i]) { | |
| return false | |
| } | |
| } | |
| return true | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment