0
 function palindrome(str) { // Good luck! var a = str.replace(/\s|[0-9_]|\W|[#$%^&*()]/g, "").toLowerCase(); if (a === a.split("").reverse().join("")) { return true; } return false; } palindrome("eye"); palindrome("1 eye for of 1 eye.") //should return false. 

I have done this task on freecodecampus.com. Can anyone tell me why it should give false? If we are removing dot and punctuations, then isn't it right that it should return true?

5
  • 1
    Instead of having us speculate - why don't you ask the authors of that task? VTC as unclear. Commented Jun 23, 2017 at 12:01
  • Is removing numbers the part of the task? Commented Jun 23, 2017 at 12:24
  • Exactly Ilya, here is whole text; Commented Jun 23, 2017 at 12:25
  • Check for Palindromes Return true if the given string is a palindrome. Otherwise, return false. A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing. Note You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes. We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others. We'll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2". Commented Jun 23, 2017 at 12:25
  • This regex looks overcomplicated for the job asked: "you'll need to remove all non-alphanumeric characters". What about /[^a-zA-Z0-9]+/g instead, which literally matches non-alphanumeric characters? Commented Jun 23, 2017 at 14:32

4 Answers 4

1

According to your comment "Note You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols)", you have to keep alphanumeric characters (ie. letters AND digits). So remove NON alphanum characters (ie. [\W_]). \W is the negation of \w: [^a-zA-Z0-9_]

This is done with:

var test = [ "racecar", "RaceCar", "race CAR", "2A3*3a2", "2A3 3a2", "2_A3*3#A2", "1 eye for of 1 eye." ]; function palindrome(str) { var a = str.replace(/[\W_]+/g, "").toLowerCase(); if (a === a.split("").reverse().join("")) { return true; } return false; } console.log(test.map(function (a) { return a+' : '+palindrome(a); }));

Sign up to request clarification or add additional context in comments.

Comments

1
function palindrome(str) { // Good luck! var a = str.replace(/\s|[0-9_]|\W|[#$%^&*()]/g, "").toLowerCase(); // Here print a // a = "eyeforofeye"; which is perfect palindrome if (a === a.split("").reverse().join("")) { // will pass this condition return true; } return false; } palindrome("1 eye for of 1 eye.") 

See my comments in the code. The replace method is using a regex to replace all numbers, special character and spaces with nothing. So all you get is a single word with no spaces, numbers and special characters.

In your case you will get eyeforofeye which is perfect palindrome.

Comments

1

You are doing a Rube Goldberg process by providing an overly complicated Regular Expression which could be shorten to /[^a-z]/ and it doesn't return false if you execute your code.

function palindrome(str) { var a = str.replace(/[^a-z]/ig, '').toLowerCase(); return a === a.split('').reverse().join(''); } console.log(palindrome('race CAR')); console.log(palindrome('2A3 3a2')); console.log(palindrome('eye')); console.log(palindrome('1 eye for of 1 eye.')); console.log(palindrome('stack'));

1 Comment

among simplest ways, the best simplest ``` /[^a-z]/ig``` really good logic, if we need just letters why not to write caret character all letters ...
0

Thanks a lot folks, have done it; Also got some good information on RegeXes. Reading RegEx from Eloquent Javascript, can anyone suggest another better source? Thanx ahead By the Way As an Answer it took this, ( for those who are interested in answer that passes all ticks in project) ,

function palindrome(str) { // Good luck! var a = str.replace(/[^a-z0-9]/ig, "").toLowerCase(); if (a === a.split("").reverse().join("")) { return true; } return false; } palindrome("eye"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.