Palindrome Checker help with Filter

Hello, I recently finished the challenge. However, I came across something interesting that I don’t really understand and I need help understanding the problem.

My approach to the problem was to convert the string into an array. Afterwards, I wanted to use the .filter() and .test() method to remove the non alphanumeric elements of the array. This was my initial code:

function palindrome(str) {
  let alphaNumeric = /([a-z\d])/g
  let arr = str
    .filter(letterOrNumber => alphaNumeric.test(letterOrNumber));


My problem was that the filter did not work properly. It alternately filtered the letters, instead of just filtering the letters. For example, when the string input was eye, the .test() method return false for y, and true for both e’s. When I inputted ‘racecar’, ‘r’ returned true, ‘a’ returned false, ‘c’ returned true, ‘e’ returned false, etc. I discovered that the global flag (g) was the problem. Can anyone help me understand why the global flag caused the alternating filter?

take a look at the documentation on test

oh wow this makes a lot of sense now. Thanks so much!