Skip to content

Instantly share code, notes, and snippets.

@arrbxr
Created February 18, 2018 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrbxr/b65f75268ebdae80a1900b6047bf9835 to your computer and use it in GitHub Desktop.
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
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