Created
March 27, 2024 10:29
-
-
Save juque/01193e0e5ec312f60fd33c8a15b1873b 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
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