Check for Palindromes help

function palindrome(str) {
  // Good luck!
  var lowerCase = str.toLowerCase(); //Make string lowercase
  var myArr = lowerCase.split(""); //Make string an array
  var formatArr = myArr.filter(function(val) { //Filters out spaces
  return val !== (" "); 
  var reversedArr = formatArr.slice().reverse(); //Reverses the array
  var origStr = formatArr.join("");  //Turn arrays back to strings
  var reverseStr = reversedArr.join("");
  if (origStr === reverseStr) { //Compares the strings
    return true;
  else {
    return false;


I’m most of the way through this exercise. I’m stuck because certain tests are failing, the ones with special characters like “_” or “,” As you can see in my code I worked out a way to remove spaces, but can’t for the life of me work out how to omit special characters so that just alphanumeric characters can be compared. Anyone able to point me in the right direction?

You can modify your filter callback by exhaustively filtering out non-letters (using a long if-else block, or using logical operators), but that’s exhausting.

Or modify that callback and check if the character’s ASCII code falls in the range of the codes for letters.

Or use regex.

You can use a simple regex to replace all the characters that are not alphanumeric. I want you to try to make the regex on your own that’s the reason i am just providing you with a hint.

function palindrome(str) {
  // Good luck!
  str=str.replace(/\s/g, '');
  str=str.replace(/,/g ,'');
  str=str.replace(/0/g ,'');
  var tmp = str.toLowerCase();
  var con= [];
  con =tmp.split('');
  con = con.reverse();
  con = con.join('');
  if (con === tmp){
    return true;
    return false;

Try simplifying the regex expression into one or two lines. 

Please don’t go copying and pasting the sample code above. The idea is to get a feel of the steps taken and come out with a solution of your own

1 Like

I got it! I changed the code to remove the filter line which only handled spaces and did a


Which filtered the whitespace and unwanted characters. Thank you for the hints! :smiley:

1 Like

Glad you figured it out. I did exactly the same thing. Cheers! :beers: