Check for Palindromes - can't filter the digits

Tell us what’s happening:

can’t find out why ‘filtered’ var can’t filter digits from ‘toReduce’ array? I’ve tried to filter letters from the ‘toReduce’ array like “o” and it works out. On the other hand I just can’t filter digits. Anyone can help here?

Your code so far

function palindrome(str) {
  // Good luck!
  var lowLetter = str.toLowerCase();
  var toReduce = lowLetter.split("");
  var filtered = toReduce.filter(function(x) {
    return x != /\d/gi;
  return filtered;

palindrome("8not a palindrome8");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:

To remove characters from a string using regex, use the .replace() function on the string.

var filtered = toReduce.replace(/\d/gi, '');
1 Like

If you wanted to use the filter method like you have though, you can tweak your existing code a little, using the .test() method for regex values:

1 Like

Thank you. Never heard about this ‘test’ method. I’m pretty sure it’s something that wasn’t explained in previous lessons. Anyway, I’ll test this!

There’s loads missing from the lessons - they just get you started really.

Reading the documentation for all the different languages / frameworks / technologies you encounter is key - you can dig up all sorts of treasures in those.

1 Like