Palindrome Checker, returning the correct true/false value for regex


I am having a problem with the for loop returned values. I don’t get the same results(true/false) when I check the array elements individually(1) and when I use for loop. Also, just realized that if I add the statement (1) just before the for loop statement and use iteration numbers(i), it gives the correct return value for the statement(2).

This is very confusing. I might be missing a point, and for that, I am sorry to waste your time.

Please note that I was at the beginning of solving the challenge, it is not the final version of my code and my regular expression may not be correct.

Thank you.

function palindrome(str) {
let spltArr = [];

spltArr = str.toLowerCase().split("");

let myRegex = /[^a-z0-9]/ig;

//console.log(myRegex.test(arr[1]));  // (1) check the array elements individually

for(let i = 0; i < spltArr.length; i++){
//console.log(myRegex.test(arr[i])); //(2) when I use this line together with the 
//statement below, this line give the correct value(true/false)but the statement 
//below only returns "false".
 console.log("i is " + i + " spltArr[i] is " + spltArr[i] + " " + myRegex.test(spltArr[i]));


palindrome("%%%%%%&=Ey e_");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36.

Challenge: Palindrome Checker

Link to the challenge:

Just taking a brief look at this, it is asking you to remove all non-alphanumeric characters. I’m not sure if your logic is to loop through the entire array, check if the character is non-alphanumeric, and remove it? I think a really nice solution might be to RegEx the word, and then use the .replace() method!

This would remove testing every character individually if it is alphanumeric or not. Just a thought, maybe?

Hi caleb-mabry,

Thank you for your answer.

Yes, you are right; my regex or the way I want to solve the challenge may not be correct but I am just wondering why I don’t get the same true/false values when I check the array in 2 different ways; individually and for loop. The for loop actually goes through each array element but only evaluates some of them.

The first thing that jumps out at me is that you are using the g flag. Do you know what it is and have a specific reason for using it? Are you aware of the problems it can cause?

1 Like

Hi ArielLeslie,

Thank you very much for your answer. It solved my problem.

I think “g” flag searches whole string. If I don’t use it would just test the first. However, if I don’t use it :smiley: I get the correct values for my regex check. By splitting the string actually I don’t need to use it as there is only one character to check. However, I don’t understand why it causes this problem as there is only one character to check in each element of the array during each iteration.

Good job on the challenge! I would recommend taking this opportunity to learn about why the global tag was breaking your solution. It’s not quite what you think it is. This article might be a good place to start and I think that there are some pretty good StackOverflow discussions on cases just like yours.

1 Like


That’s Brilliant! Thank you. I would like to understand “why”.

Hopefully, your help will help me to finish the challenge.

Have a good day and a weekend.

I’m glad I could help. Happy coding!