# Help to understand Advanced palindromes solution

Help to understand Advanced palindromes solution
0

#1

Hey guys, I managed to solve this but I would like to be able to understand how the advanced solution works. I just can’t make the step by step process clear in my head. Could somone please explain in more detail than the comments of the code how each step works?? I leave the code here just in case. Thanks!

`````` //this solution performs at minimum 7x better, at maximum infinitely better.
//read the explanation for the reason why. I just failed this in an interview.
function palindrome(str) {
//assign a front and a 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) {
//increments front pointer if current character doesn't meet criteria
while ( str[front].match(/[\W_]/) ) {
front++;
continue;
}
//decrements back pointer if current character doesn't meet criteria
while ( str[back].match(/[\W_]/) ) {
back--;
continue;
}
//finally does the comparison on the current character
if ( str[front].toLowerCase() !== str[back].toLowerCase() ) return false
front++;
back--;
}

//if the whole string has been compared without returning false, it's a palindrome!
return true;
``````

}

#2

I understand this is much faster than alot of the solutions I see on FCC, but what I am not sure is why using a while with a continue would be faster than a simple if statement (see below for what I think would be equivalent. Maybe someone else can explain why the while with continue would or would not be faster than the if statements I used below.

``````function palindrome(str) {
//assign a front and a 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) {
//increments front pointer if current character doesn't meet criteria
if ( str[front].match(/[\W_]/) ) {
front++;
}
//decrements back pointer if current character doesn't meet criteria
if ( str[back].match(/[\W_]/) ) {
back--;
}
//finally does the comparison on the current character
if ( str[front].toLowerCase() !== str[back].toLowerCase() ) return false
front++;
back--;
}

//if the whole string has been compared without returning false, it's a palindrome!
return true;
}
``````