Skip to content

Instantly share code, notes, and snippets.

@juque
Created March 27, 2024 10:29
Show Gist options
  • Save juque/01193e0e5ec312f60fd33c8a15b1873b to your computer and use it in GitHub Desktop.
Save juque/01193e0e5ec312f60fd33c8a15b1873b to your computer and use it in GitHub Desktop.
function isPalindrome(str) {
str = str.replace(/[^a-z0-9]/i, '').toLowerCase();
if ( str.length === 0) {
return true;
}
let left = 0;
let right = str.length - 1;
while ( left < right ) {
if ( str[left] !== str[right] ) {
return false;
}
left++;
right--;
}
return true;
}
console.log(isPalindrome('kayak'));
console.log(isPalindrome('radar'));
console.log(isPalindrome('letters'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment