Palindrome Checker Can't Do Spaces

Palindrome Checker Can't Do Spaces
0

#1

I’m having trouble with the palindrome checker challenge. My function works fine for the first few test cases, but fails as soon as it encounters spaces, like “race car.” According to the challenge rules, the checker should ignore non-alphanumeric characters and convert everything to lowercase, which I’ve done, but for some reason the section of the code that removes non-alphanumeric characters doesn’t seem to work on spaces, despite the fact that spaces don’t meet /^[a-z0-9]/ and I’ve said to remove things that don’t meet those requirements. Any input with spaces in it is automatically labelled not a palindrome. What’s wrong here?

function palindrome(str) {
  // Make an array
  arr = str.split('');

  // Make array all alphanumeric and lowercase
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] != /^[a-z0-9]/) {
      arr.splice(i, 1);
    }
  }
  str = arr.join("");
  str = str.toLowerCase();

  // Test if palindrome
  for (var j = 0; j < Math.floor((str.length + 1) / 2); j++) {
    if (str[j] != str[str.length - 1 - j]) {
      return false;
    }
  }

  return true;
}

#2

OK, your if statement:

if (arr[i] != /^[a-z0-9]/) {

has two problems. First, I assume you are wanting to use a regular expression to test if arr[i] is NOT a letter or number. If so then you need to use RegExp.prototype.test function like.

regexObj.test(str) // results in a true or false depending on whether str matches the regular expression (regexObj).

Now, you are close on the regular expression you would use, but you need to move the ^ inside the [ and add i after the second / to ignore case. It should look like:

/[^a-z0-9]/i

Putting it all together your if statement would look like:

if (/[^a-z0-9]/i.test(arr[i]) {

#3

That helped a lot. Thank you. One more problem, it doesn’t always work with one try. It will leave some special characters and the only way I’ve founded to fix it is to filter it out again.


#4

Nevermind. I found a better way using filter and the regex you helped me with, now to whole thing looks like this:

function palindrome(str) {
  // Make an array
  str = str.toLowerCase();
  arr = str.split('');

  // Make array all alphanumeric and lowercase
  arr = arr.filter(function(char) {
    return !/[^a-z0-9]/.test(char);
  });

  str = arr.join('');

  // Test if palindrome
  for (var j = 0; j < Math.floor((str.length + 1) / 2); j++) {
    if (str[j] != str[str.length - 1 - j]) {
      return false;
    }
  }

  return true;
}

Thank you so much for your help. Much appreciated.


#5

Glad you figured it out. FYI, below is how you could have used the splice you were trying to use. Because splice removes an element from the array, you need to decrease i or the next element would be skipped from the if statement evaluation.

  for (var i = 0; i < arr.length; i++) {
    if (/[^a-z0-9]/i.test(arr[i])) {
      arr.splice(i--, 1); // here we back one index because we just deleted one element
    }
  }