Need detailed explanation for how match function is used in Advanced solution of Check for Palindromes

Need detailed explanation for how match function is used in Advanced solution of Check for Palindromes
0

#1

Hey guys, I have been trying to rap my head around the advanced solution, and did not find really any comments about it other than that it is more efficient. Specifically, I have been trying to understand how the match works in this problem. A step by step explanation is much needed, and I think would be much appreciated by all the beginner level campers. Thanks!


Solution Explanation please?: Check for Palindromes
#2

I moved your question to a separate topic, because your question pertains to a different solution than the original poster need help with.

First, take a look at the match documentation and if you still have questions, be specific about what part of the use of match you do not understand in the advanced solution.


#3

I finally wrapped my head around the use of match thanks to the documentation but now the issue i am having is with the while loop (back > forth). If we take as an example:

palindrome(“A man, a plan, a canal. Panama”) should return true.

It goes through the first iteration, matching “a” for “a” then iterating to the next loop.

after the first pass:

front:1
back:2

in the second pass we see a space, which meets the criteria for
while ( str[front].match(/[\W_]/), which then adds ++ to front, which then matches
"m" for “m” and then iterating to the next loop

after the second pass:

front:3
back:3

because it doesn’t meet the criteria for the while loop (back > forth) it stops iterating and returns true, having only matched “am”.

Am I reading something wrong here?


#4

Why do you think back is 2 after the first pass?

front starts at 0 and back starts at 29. At the end of the outer while loop (back > front), front is 1 and back is 28.

Put some console.log statements in for front and back so you can see what they are through the code execution.