Help for Check for Palindromes challenge

Help for Check for Palindromes challenge
0

#1

My code is not working for sentences with punctuation please help


#2

The problem is with your regex. Instead of trying to select every bit of punctuation, it would be much easier to select characters that are NOT a-z or 0-9:

// this will not work exactly, make sure to make it match everything, etc.
[^a-z0-9]

Also, another tip:

return newStr === str

instead of that if-else chain. Also, it would be better to use the strict equals here: === instead of ==


Here is a simple one-liner:

function palindrome(str) {  return str.toLowerCase().replace(/[^a-z0-9]+/g, '') === str.toLowerCase().replace(/[^a-z0-9]+/g, '').split('').reverse().join('') }

#3

Your code, in short, works like this:

is str equal to str.replace(/[.,?!etc]/, '').toLowerCase()?

See the mistake?
You can easily spot such errors by using the console (depending on your browser/os its somewhere in the menu/options/F12). insert this in at line 6 and you will see the difference in the console:

console.log('str:', str, 'newStr:', newStr);


#4

Thanks guys i figured it out now :slight_smile:


#5

Another approach: … str.replace(/[\s\W_-]/g, ‘’).toLowerCase();