Created
February 18, 2018 19:53
-
-
Save arrbxr/b65f75268ebdae80a1900b6047bf9835 to your computer and use it in GitHub Desktop.
Check for Palindromes created by arrbxr - https://repl.it/@arrbxr/Check-for-Palindromes
This file contains 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 palindrome(str){ | |
// assign a front and back pointer | |
let front = 0; | |
let back = str.length - 1; | |
//back and front pointers won't always meet in the middle, so use (back > front) | |
while(back > front){ | |
while(str[front].match(/[\W_]/)){ | |
//increments front pointer if current character doesn't meet criteria | |
front++; | |
continue; | |
} | |
while(str[back].match(/[\W_]/)){ | |
//decrements back pointer if current character doesn't meet criteria | |
back--; | |
continue; | |
} | |
//finally does the comparison on the current character | |
if(str[front].toLowerCase() !== str[back].toLowerCase()) | |
return false; | |
else | |
front++; | |
back--; | |
} | |
//if the whole string has been compared without returning false, it's a palindrome | |
return true; | |
} | |
palindrome("EYE"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment