Help w/ palindrome checker & filtering arrays

Tell us what’s happening:

I am on the palindrome checker challenge. At first it seemed impossible, but then I remembered one of the first algorithm I ever did: the reverse a string one.

My idea is to split the string into an array, filter out all of the symbols and numbers, reverse it and check if it matches the given string’s letters.

I need help on filtering out the symbols. I reviewed the challenge that taught about filtering. I tried that, but it didn’t work. Can you helpfully give me a hint?

Thanks.

Your code so far


function palindrome(str) {
// Good luck!
var newStrArray = str.split('');

//what do I put here?
var palindromeArray = str.filter(letters => letters !== /[^a-z]/i);
return palindromeArray.reverse().join('');
}



console.log(palindrome("eye"));

Your browser information:

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

Challenge: Palindrome Checker

Link to the challenge:

hi @ConnerOw1115
I have noticed that you are using a regular expression to filter out non-alphabetic characters, but you are testing if letters is different then the regexp itself, try using the test() method on the regex like

/[^a-z]/.test(letters)

find more about the test() method: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/RegExp/test

You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.

That was very helpful, but I didn’t get the right solution just yet. I’ve tried filtering out the array with the method you shared with me, but it didn’t quite work.

function palindrome(str) {
  // splits the array
  var newStrArray = str.split('');

  //filters the array of all symbols, punctuation marks, and spaces
  //I'm stuck here
  var palindromeArray = newStrArray.filter(letters => letters !== /[^a-z]/.test(letters)); 

  
  return palindromeArray;
}



console.log(palindrome("e#ye"));

Could you please tell me what I’m doing wrong here with the filter method?

what does the test method return? would that ever be equal to a string?

if you don’t know what test return you may need to review regular expressions

I tested it and the test method returns a boolean. I decided to use the .match() method because it returns an array. I tried using this:

var symbols = str.match(/[^a-z]/g);
var palindromeArray = newStrArray.filter(letters => letters !== symbols);

but the code I tried did nothing to the array.

I noticed that if I used something like this:
var palindromeArray = newStrArray.filter(letters => letters !== "e");

then it would filter all of the letter “e” out.

I really do not have any Idea what is going on with my code and why it is not working.

isn’t this useful? what does filter expect from its callback?

because you are always trying to compare stuff that shouldn’t be compared

So what I’m doing wrong is comparing the letters functional variable to an array like this?
letters => letters !== ["#","$"," "]

I’ve tried iterating through the array with a for loop but it still didn’t work.

for(var i = 0; i < symbols.length; i++){
var palindromeArray = newStrArray.filter(letters => letters !== symbols[i]);
}

what exactly your loop is doing?
look at it carefully
you are throwing stuff together haphazardly hoping it will work, without thinking of what this step should actually be doing, and what’s the best instrument

let arr = [0,1,2,3]
for (let i = 0; i < arr.length; i++) {
 var newArr = arr.filter(el => el != i)
}
console.log(newArr);

what do you expect newArr to be? why?
what is actually is? why? think about it

@ConnerOw1115
HINT:
“The test() method executes a search for a match between a regular expression and a specified string” _MDN
test() returns a boolean value (true if there is a match , false otherwise).

thank you but I already know, I don’t need those hint - I already knew that

sorry ! a taged the wrong person ! :smile:

function palindrome(str) {
str = str.toLowerCase().replace(/[\W_]/g, “”);

for (var i = 0; i < parseInt(str.length / 2); i++){
if (str[i] !== str[str.length - i - 1]){
return false
}
}
return true;
}