Skip to content

Instantly share code, notes, and snippets.

@onejohi
Created January 14, 2025 19:09
Show Gist options
  • Select an option

  • Save onejohi/e46ddfdc03fedbfefeec6396fc3d1e1a to your computer and use it in GitHub Desktop.

Select an option

Save onejohi/e46ddfdc03fedbfefeec6396fc3d1e1a to your computer and use it in GitHub Desktop.
//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