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.
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
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 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).