Palindrome - regular expression white space issue

Tell us what’s happening:
Describe your issue in detail here.
Hello,
I am struggling to correctly handle the strings whitespace within my regular expression.
Should my approach be to remove the the whitespace, or to match it?
My thanks in advance for any help you can give me.
Pierson
Your code so far


function palindrome(str) {
let regex = (/[^a-zA-Z]/gi);
str = str.replace(regex, "");
let len = str.length;

for (let i = 0; i < len/2; i++) {
  if (str[i] !== str[len - 1 - i]) {
    return false;
  }
}
return true;
}

console.log(14, palindrome("eye"), "=  eye");
console.log(15, palindrome("_eye"), "= _eye");
console.log(16, palindrome("race car"), "= race car");
console.log(17, palindrome("not a palindrome"), "= not a palindrome");
console.log(18, palindrome("A man, a plan, a canal. Panama"), "= A man, a plan, a canal. Panama");
console.log(19, palindrome("never odd or even"), "= never odd or even");
console.log(20, palindrome("nope"), "= nope");
console.log(21, palindrome("almostomla"), "= almostomla");
console.log(22, palindrome("My age is 0, 0 si ega ym."), "= My age is 0, 0 si ega ym.");
console.log(23, palindrome("1 eye for of 1 eye."), "= 1 eye for of 1 eye.");
console.log(24, palindrome("0_0 (: /-\ :) 0-0"), "= 0_0 (: /-\ :) 0-0");
console.log(25, palindrome("five|\_/|four"), "= five|\_/|four");

palindrome("eye");

  **Your browser information:**

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

Challenge: Palindrome Checker

Link to the challenge:

No, I don’t think the spaces are the issue

You are failing three tests

1

palindrome("A man, a plan, a canal. Panama") should return true .

If I console.log the string you are looping over by adding a console.log(str) on line 5, the console shows AmanaplanacanalPanama which is a palindrome, if not for the fact that 'A' === 'a' is false, and you are not ignoring case as instructed

2

palindrome("My age is 0, 0 si ega ym.") should return true .
Same issue as above

3

palindrome("1 eye for of 1 eye.") should return false .
This has a different issue, the console.log prints eyeforofeye which is a palindrome, but…

You’ll need to remove all non-alphanumeric characters

You also removed numbers, if you keep the numbers that string is no more a palindrome

1 Like

Thank you for such a fast response ieahleen, I really appreciate your help. I’ll try to implement your great instructions. Thank you again. Pierson

ieahleen, I realised when reading your valuable feedback that I had not properly read the project Instructions. Hehehehe…I feel such an idiot. :wink: Thanks again for your help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.